繁体   English   中英

ggPlot2图例调整

[英]ggPlot2 legend adjustment

我正在使用ggplot为10个主题绘制散点图,每个主题在两个不同的列中包含2个值。 我能够绘制散点图,但无法为其调整图例。 图例框应显示供体1,供体2 ...,供体10,但改为显示供体1,供体10,...,供体9。 另外,它还显示了图例中geom_point函数中提到的大小块,该块不应存在。

以下是我的代码段:

color1 = 'red'
color2 = 'green'
color3 = 'blue'
color4 = 'forestgreen'
color5 = 'purple'
color6 = 'yellow'
color7 = 'orange'
color8 = 'royalblue'
color9 = 'palevioletred4'
color10 = 'pink'

 p2<-qplot(CEACAM4.2hrs, CEACAM4.EDTA, data=res, xlab="CECAM4   PAX 2 Hr", ylab=" CECAM4 EDTA 0 Hr")


 p3<-p2+geom_point(aes(color = row.names(res2), size = 3))  + scale_x_continuous(limit =c(22,28), breaks = c(22,23,24,25,26,27,28)) + scale_y_continuous(limit=c(22,28), breaks = c(22,23,24,25,26,27,28)) +  stat_smooth(method="lm", se=FALSE)+ scale_color_manual(values=c(color1, color2, color3, color4, color5, color6, color7, color8, color9, color10))

 p4<- p3 + guides(colour = guide_legend(override.aes = list(size = 10)))

 p4+ ggtitle("CECAM4 EDTA 0Hr Vs PAX 2Hr") +  theme(plot.title = element_text(size = 28,colour=" mediumvioletred", face = "bold"))+ theme(legend.key = element_rect(colour = "black"), legend.title=element_text(size=22), legend.text=element_text(size=20)) + theme(plot.margin=unit(c(2.5,2,2,2),"cm"))+ theme(axis.text=element_text(size=18, face="bold"), axis.title=element_text(size=20, face="bold", colour="purple4")) + theme(axis.title.y=element_text(vjust=-0.65)) + theme(axis.title.x=element_text(vjust=-3.0)) + theme(plot.title = element_text(vjust=5)) + theme(axis.ticks.length=unit(1,"cm")) + theme(legend.key.size=unit(1.5,"cm"))

 ggsave(file="ggplot_cecam4_2_edta.png")

因此,更准确的问题是,如何使用ggplot依次获取图例? 例如

供体1

供体2

供体3

供体4

供体5

供体6

供体7

供体8

供体9

供体10

谢谢..

您没有提供数据来测试您的代码...我看到的图例是在

color = row.names(res2)

所以我想row.names包含Donor-1,Donor-2等。它们是字符,因此请尝试使其成为因子

color = factor(row.names(res2), levels=c("Donor-1", "Donor-2", etc.))

factor启用矢量的自定义顺序)

暂无
暂无

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

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