为条形图和线条创建共同的图例 [英] creating a common legend for bar plot and line
问题描述
我是R的新手,所以如果这是一个非常基本的问题,我会提前道歉。
我正在尝试绘制一个图表,显示放电和暂停沉积物负荷(SSL)。但是,我想弄清楚条形图表示放电,而线图表示SSL。我有两个想法:
-
给放电标签和SSL上色分别对应于条形图和折线图,因此读者可以直观地知道哪个属于哪个。但是ggplot2不允许我这样做,因为它会将两个y轴的颜色都设为相同的颜色。
-
建立一个图例,清楚地表明红线属于SSL,蓝盒图属于排放。
这是我的脚本:
库(ggplot2)
库(gridExtra)
库(RColorBrewer)
库(tibble)
P_Discharge;-每月Pyay $ mean.discharge; b $ b P_MaxTemp小于$ -Pyay $ mean每月的最高温度
P_MinTemp <-每月Pyay $ mean.minimum 。温度
P_Rain<-Pyay $每月最大降雨量。
P_SSL<-Pyay $ Mean.suspended.sediment.load
#重新排序月份
Pyay $月<-factor(Pyay $ Month,
level = c( Jan, Feb, Mar, Apr, May, Jun,
Jul, Aug, Sep, Oct, Nov, Dec))
#PlottingdischargeandSSL
Pgraph1<-ggplot(Pyay,aes(x = Month,group = 2))
Pgraph1<-Pgraph1 + geom_bar(aes(y = P_Discharge),stat = identity,fill = blue)
Pgraph1<-Pgraph1 + geom_line(aes(y = P_SSL),colour = red,size = 1)+
labs(y = expression(Q /(m ^ {3})) )+
labs(x =)
#addingsecondaxis
Pgraph1<-Pgraph1 + scale_y_continuous(sec.axis = sec_axis(〜。,name = expression(
暂停〜沉积〜加载〜(10 ^ {6}〜t))))
#colouringaxistitles
Pgraph1<-Pgraph1 + theme(axis.title.x = element_blank( ),
axis.title.y = element_text(size = 14),
axis.text.x = element_text(size = 14))
Pgraph1
数据
Pyay<-tibble :: tribble(
〜月,〜平均每月排放量,〜平均每月最高温度,〜平均暂停。沉积物负荷,〜最大每月降雨量,〜平均每月最小温度,
扬,8.528,32.2,3.407,1.5, 16.2,
二月,6.316、35.1、2.319、0.9、17.8,
三月,7、37.6、2.587、5.1、21.2,
四月,8.635、38.7, 3.573,27.3,24.7,
五月,12.184,36,5.785,145.1,25.6,
六月,30.414,31.9,21.811,234.8,24.8,
七月, 70.753、31、70.175, 198,24.8,
八月,79.255,31,81.873,227.5,24.7,
九月,67.079,32.3,65.798,205.7,24.6,
八月,53.677, 33.5、47.404、124、24.2,
十一月,22.937、32.7、14.468、56、21.7,
十二月,12.409、31.5、5.842、1.5、18.1
)
解决方案如果您将<您将在
aes()
中使用code> color 和fill
自变量得到一个传说。使用scale_fill_manual
,我们将条形更改为蓝色。将颜色
和填充
labs()
设置为##绘制排放和SSL
Pgraph1<-ggplot(Pyay,aes(x = Month,group = 2))
Pgraph1<-Pgraph1 + geom_bar(aes(y = P_Discharge,fill = discharge),stat = identity)
Pgraph1<-Pgraph1 + geom_line(aes(y = P_SSL,color = SSL),size = 1)+ labs(y = expression(Q /(m ^ {3})))+ labs( x =)
Pgraph1<-Pgraph1 + scale_fill_manual(values = c( discharge = blue))+ labs(color =,fill =)
#添加第二个轴
Pgraph1<-Pgraph1 + scale_y_continuous(sec.axis = sec_axis(〜。,name = expression(Suspended〜sediment〜load〜(10 ^ {6}〜t))))
#着色轴标题
Pgraph1<-Pgraph1 +主题(
axis.title.x = element_blank(),
axis.title.y = element_text(size = 14),
axis.text.x = element_text(size = 14)
)
Pgraph1
I'm very new to R so I apologise in advance if this is a very basic question.
I'm trying to plot a graph showing discharge and suspended sediment load (SSL). However, I want to make it clear that the bar plot represents discharge, and the line graph represents SSL. I had two ideas:
color the label for discharge and SSL to correspond to the bar graph and line graph respectively, so the reader intuitively knows which one belongs to which. but ggplot2 doesn't allow me to do this because it'll colour both y axes the same color.
build a legend that indicates clearly that the red line belongs to SSL, blue box plot belongs to discharge. There is a post here which does something similar but I can't seem to achieve it. I would greatly appreciate it if someone could help me out.
This is what my graph looks like now.
This is my script:
library(ggplot2) library(gridExtra) library(RColorBrewer) library(tibble) P_Discharge <- Pyay$Mean.monthly.discharge P_MaxTemp <- Pyay$Mean.monthly.max.temperature P_MinTemp <- Pyay$Mean.monthly.minimum.temperature P_Rain <- Pyay$Max.monthly.rainfall P_SSL <- Pyay$Mean.suspended.sediment.load #reorderingthemonths Pyay$Month <- factor(Pyay$Month, levels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) #PlottingdischargeandSSL Pgraph1 <- ggplot(Pyay, aes(x=Month, group=2)) Pgraph1 <- Pgraph1 + geom_bar(aes(y=P_Discharge), stat="identity", fill="blue") Pgraph1 <- Pgraph1 + geom_line(aes(y=P_SSL), colour="red", size=1) + labs(y=expression(Q/(m^{3}))) + labs(x="") #addingsecondaxis Pgraph1 <- Pgraph1 + scale_y_continuous(sec.axis=sec_axis(~., name=expression( Suspended~sediment~load~(10^{6}~t)))) #colouringaxistitles Pgraph1 <- Pgraph1 + theme(axis.title.x=element_blank(), axis.title.y=element_text(size=14), axis.text.x=element_text(size=14)) Pgraph1
Data
Pyay <- tibble::tribble( ~Month, ~Mean.monthly.discharge, ~Mean.monthly.max.temperature, ~Mean.suspended.sediment.load, ~Max.monthly.rainfall, ~Mean.monthly.minimum.temperature, "Jan", 8.528, 32.2, 3.407, 1.5, 16.2, "Feb", 6.316, 35.1, 2.319, 0.9, 17.8, "Mar", 7, 37.6, 2.587, 5.1, 21.2, "Apr", 8.635, 38.7, 3.573, 27.3, 24.7, "May", 12.184, 36, 5.785, 145.1, 25.6, "Jun", 30.414, 31.9, 21.811, 234.8, 24.8, "Jul", 70.753, 31, 70.175, 198, 24.8, "Aug", 79.255, 31, 81.873, 227.5, 24.7, "Sep", 67.079, 32.3, 65.798, 205.7, 24.6, "Oct", 53.677, 33.5, 47.404, 124, 24.2, "Nov", 22.937, 32.7, 14.468, 56, 21.7, "Dec", 12.409, 31.5, 5.842, 1.5, 18.1 )
解决方案If you put the
color
andfill
arguments inside theaes()
you'll get a legend. Withscale_fill_manual
we change the bars to blue. setting thecolor
andfill
labs()
to""
removes them.## Plotting discharge and SSL Pgraph1 <- ggplot(Pyay, aes(x=Month, group = 2)) Pgraph1 <- Pgraph1 + geom_bar(aes(y=P_Discharge, fill = "discharge"), stat="identity") Pgraph1 <- Pgraph1 + geom_line(aes(y=P_SSL, colour = "SSL"), size=1)+ labs(y=expression(Q/(m^{3}))) + labs(x=" ") Pgraph1 <- Pgraph1 + scale_fill_manual(values = c("discharge" = "blue")) + labs(color = "", fill = "") #adding second axis Pgraph1 <- Pgraph1 + scale_y_continuous(sec.axis = sec_axis(~.,name = expression(Suspended~sediment~load~(10^{6}~t)))) #colouring axis titles Pgraph1 <- Pgraph1 + theme( axis.title.x = element_blank(), axis.title.y = element_text(size=14), axis.text.x = element_text(size=14) ) Pgraph1
这篇关于为条形图和线条创建共同的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!