ggplot:如何为不同于连接线的点着色
ggplot: how to color points different than the connecting line
我正在尝试创建一个散点图,其中的点由一条线连接。但是,我希望在线条保持黑色时将点着色。如果它尝试它,我要么没有传说。或者我得到一个部分或完全缺失的图例:
structure(list(Strain = c("Wildtype", "Wildtype", "Wildtype",
"Wildtype", "Wildtype", "Wildtype"), Time = c(0L, 24L, 48L, 72L,
120L, 144L), m = c(0, 1.2, 3.4, 6.5,
11.1, 14.2), sd = c(0, 1, 1,
1, 1, 1)), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"))
ggplot(data=Wildtype, aes(x=Time,y=m))+
geom_errorbar(aes(ymin=m-sd, ymax=m+sd), width=1,
position=position_dodge(.9))+
geom_line(aes(color=Strain))+
scale_color_manual(values=c((Strain="black")))+
geom_point(aes(color=Strain),fill="red",color="black", shape =22, size=2.5,stroke=0)+
xlab("Time [h]")+
ylab("Hydrogen [µmol]")+
scale_x_continuous(breaks=seq(0,150,20))+
scale_y_continuous(breaks=seq(0,25,5),limits=c(0,25))+
ggtitle("Title")+
theme(plot.title = element_text(hjust = 0.5))
df 基本上由一个“应变”组成,它是一个字符、时间和特定时间的测量值。
这段代码引导我找到我想要的图表类型,但它缺少图例中的红色框。我只有一条黑线给我应变。如果我删除 scale_color_manual 线,我会得到一条红线和图例中的一条红线(该线没有交叉点)。是否有机会将不同“菌株”的所有线条涂成黑色,仅将数据点涂成彩色。但是图例中的线和点都有吗?
非常感谢您的帮助
最简单的方法是为 geom_line
创造一种新的美学,例如linetype
- 如果你想要黑色的线条,你甚至不需要指定它的颜色(默认为黑色)
library(ggplot2)
ggplot(data=df, aes(x=Time,y=m))+
geom_point(aes(color=Strain))+
geom_line(aes(linetype = Strain)) +
scale_color_manual(values = "red")
我正在尝试创建一个散点图,其中的点由一条线连接。但是,我希望在线条保持黑色时将点着色。如果它尝试它,我要么没有传说。或者我得到一个部分或完全缺失的图例:
structure(list(Strain = c("Wildtype", "Wildtype", "Wildtype",
"Wildtype", "Wildtype", "Wildtype"), Time = c(0L, 24L, 48L, 72L,
120L, 144L), m = c(0, 1.2, 3.4, 6.5,
11.1, 14.2), sd = c(0, 1, 1,
1, 1, 1)), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"))
ggplot(data=Wildtype, aes(x=Time,y=m))+
geom_errorbar(aes(ymin=m-sd, ymax=m+sd), width=1,
position=position_dodge(.9))+
geom_line(aes(color=Strain))+
scale_color_manual(values=c((Strain="black")))+
geom_point(aes(color=Strain),fill="red",color="black", shape =22, size=2.5,stroke=0)+
xlab("Time [h]")+
ylab("Hydrogen [µmol]")+
scale_x_continuous(breaks=seq(0,150,20))+
scale_y_continuous(breaks=seq(0,25,5),limits=c(0,25))+
ggtitle("Title")+
theme(plot.title = element_text(hjust = 0.5))
df 基本上由一个“应变”组成,它是一个字符、时间和特定时间的测量值。 这段代码引导我找到我想要的图表类型,但它缺少图例中的红色框。我只有一条黑线给我应变。如果我删除 scale_color_manual 线,我会得到一条红线和图例中的一条红线(该线没有交叉点)。是否有机会将不同“菌株”的所有线条涂成黑色,仅将数据点涂成彩色。但是图例中的线和点都有吗? 非常感谢您的帮助
最简单的方法是为 geom_line
创造一种新的美学,例如linetype
- 如果你想要黑色的线条,你甚至不需要指定它的颜色(默认为黑色)
library(ggplot2)
ggplot(data=df, aes(x=Time,y=m))+
geom_point(aes(color=Strain))+
geom_line(aes(linetype = Strain)) +
scale_color_manual(values = "red")