繁体   English   中英

ggplot 用条件颜色连接线和点 plot

[英]ggplot join line and point plot with conditional colours

 dat <- structure(list(yearID = c(2014L, 2015L, 2016L, 2017L, 2018L, 
                  2019L, 2013L), yield = c(0.328575295095694, 0.0853721681507231, 
                  1.26616635028825, 0.609179296764281, 0.519918049485935, 0.77932406868591, 
                  0.94), flag = c("sim", "sim", "sim", "sim", "sim", "sim", "obs"
                  )), row.names = c(NA, -7L), class = c("tbl_df", "tbl", "data.frame"
                  ))

  
library(ggplot2)

ggplot(dat, aes(x = yearID, y = yield, col = flag)) +
geom_point() + geom_line() + theme(legend.position = 'top')

在此处输入图像描述

如何连接所有点并使用标志列区分它们

同样在初始ggplot的一般aes()或遵循@Bas的好建议:

library(ggplot2)
#Code
ggplot(dat, aes(x = yearID, y = yield, col = flag,group=1)) +
  geom_point() + geom_line() + theme(legend.position = 'top')

Output:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM