簡體   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