1

我有 2 个简单的散点图数据集,我合并了,结果图(在 ggplot2 中)正是我想要的,除了连接数据的线。我正在努力做到这一点,所以有两条单独的虚线,有两种不同的颜色,但现在搞乱 geom_line 我只得到一条连接所有数据集的线。下面我将附上我的代码和图表的期望/当前结果。我很感激任何帮助!

dfplus<-data.frame(Mean=c(2.20e7,6.18e7,3.28e7,3.46e8,1.09e9),
               sd=c(0e0,1.05e8,4.45e7,2.26e7,0e0),Category=c("2","3","4","5","6"))
dfminus<-data.frame(Mean=c(1.86e7,3.92e6,1.30e7,8.10e7,3.20e8),
                   sd=c(0e0,5.28e6,2e7,1.41e7,0e0),Category=c("2","3","4","5","6"))

dat <- rbind(dfplus, dfminus)
dat$dataset <- factor(c(rep("dfplus", dim(dfplus)[1]), rep("dfminus", dim(dfminus)[1])))
ggplot(dat, aes(x=Category, y=Mean, color=dataset, line=dataset,group=1))+ 
scale_color_manual(values=c('#56B4E9','#E69F00'))+geom_point(size=5,shape=21,aes(color=dataset))+
theme_classic()+geom_line()

这是我想要的图表(用 MS 油漆制作,想象线条是虚线): 在此处输入图像描述 这是我当前的图表: 在此处输入图像描述

4

1 回答 1

2

你就在那儿!而不是group = 1,使用group = dataset.

ggplot(dat, aes(x=Category, y=Mean, color=dataset, 
                line=dataset,group= dataset))+ 
  scale_color_manual(values=c('#56B4E9','#E69F00'))+
  geom_point(size=5,shape=21,aes(color=dataset))+
  theme_classic()+geom_line()

在此处输入图像描述

于 2022-01-26T03:30:03.347 回答