【问题标题】:combined bar, line, and point geom in ggplots2- how to change fill on point and dashed line?ggplot2中的组合条、线和点几何-如何更改点和虚线的填充?
【发布时间】:2014-02-13 20:23:26
【问题描述】:

我在之前的帖子中对此进行了研究:Combined line & bar geoms: How to generate proper legend? 并且已经接近了。这是我使用的代码,它在条形图中添加了一条线和点几何图形:

  mort12=data.frame(
     Adj.S=c(.68,.33,.66,.62,.6,.51,.6,.76,.51,.5),
     QTL=c(1:10),
     Cum.M=c(.312,.768,NA,.854,NA,.925,.954,NA,NA,.977)
)

  ggplot(data=mort12, aes(QTL)) + 
  geom_bar(aes(y = Adj.S, color = "Adj.S"), stat="identity", fill = "red") +
  geom_point(data=mort12[!is.na(mort12$Cum.M),], aes(y = Cum.M, group = 1,size=4, color =    "Cum.M"))+  
  geom_line(data=mort12[!is.na(mort12$Cum.M),],aes(y=Cum.M, linetype="dotted",group = 1))

注意,我有一些 Cum.M 的缺失数据,因此为了连接这些点,我添加了代码以忽略缺失值)。

当我运行它时,我得到了这个数字(我不能在这里发布图片,所以它是链接的): https://docs.google.com/uc?export=view&id=0B-6a5UsIa6UpZnRZTy1OZmxrY1E

我喜欢控制线和点的外观。但是尝试使线虚线(linetype =“dotted”)并没有改变它,当我尝试改变点的填充(fill =“white”)我得到这个错误

错误:无法将连续变量映射到形状

关于如何改变线和点的属性有什么建议吗?

【问题讨论】:

  • +1 可重复性

标签: r ggplot2 bar-chart linechart


【解决方案1】:

这对我有用:

ggplot(data=mort12, aes(QTL)) + 
  geom_bar(aes(y = Adj.S, color = "Adj.S"), stat="identity", fill = "white") +
  geom_point(data=mort12[!is.na(mort12$Cum.M),], aes(y = Cum.M, group = 1,size=4, color =    "Cum.M"))+  
  geom_line(data=mort12[!is.na(mort12$Cum.M),],aes(y=Cum.M, group = 1), linetype="dotted")

我所做的只是将linetype 移出aes。一般来说,不是由你的数据驱动的aesthetics不应该在aes里面。例如,size 可能也不应该在 aes 中。

【讨论】:

    猜你喜欢
    • 2021-09-11
    • 1970-01-01
    • 2014-06-21
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多