ggplot2 将条形图(具有连续的 x 轴)放在与折线图相同的图形上
ggplot2 put a bar plot (with continous x-ax) on the same graph as a line graph
用户,
我有一个从 1 到 999 的连续 x 变量(我在下面提供了我的数据的 20 个观察样本以供使用)。
我想在同一个图上绘制一些折线图和条形图 such as this plot。我想在条形图上绘制的变量是变量 "Irrigationtotal".
我有两个问题:
1) 第一个问题是对于条形图,R 只接受 x 轴上的离散值。因此,它改变了我的连续值的顺序,例如:
1, 11, 101..., 2, 21, 22, ... 200, ... 3 33...
显然,我想要 1、2、3、4、5...
我意识到我可以使用
scale_x_discrete(限制= ...)
对于这个问题,但后来我想我将无法在与条形图相同的图上绘制折线图。
2) 第二个问题是,在共享相同的 x 轴时,我无法将它们很好地重叠在一起。
你可以在下面找到我分别为这两个图准备的代码。有人可以帮助解决上述问题吗?非常感谢!
数据
out122<- structure(list(MEt_R = c(-0.0541818151603231, -0.0562844791428272,
-0.0558715941992024, -0.0562399962945622, -0.0560460386125185,
-0.0570608897132082, -0.0569943385875705, -0.0568252787782472,
-0.0569942506473323, -0.0565197621205338, -0.056900534973487,
-0.0571427349989937, -0.0569618449465491, -0.0566601716889117,
-0.0563552308197707, -0.0568648464047371, -0.057047451157018,
-0.0571837090302319, -0.0588902340655496, -0.0592472918164029
), MEt_Irr = c(-0.0930626749780042, -0.0924059309460578, -0.0924771937440385,
-0.0905386156125412, -0.0914934037180768, -0.0898948119109486,
-0.0898827200499507, -0.090372707751177, -0.0901901622784647,
-0.0914484064620663, -0.0925147845884521, -0.0927733849042059,
-0.0960873954367445, -0.0948131376144847, -0.0955133693827158,
-0.0933133384990093, -0.0927340360155418, -0.0925138612415783,
-0.0896139882242573, -0.0912014136494108), se_MEt_Rainfed = c(0.124867384884912,
0.124157398945455, 0.124169568385358, 0.124110270348855, 0.12391954965997,
0.123742628011372, 0.123766054757713, 0.123576175335345, 0.12353428904291,
0.123443556846824, 0.122869340273675, 0.122594726299249, 0.122332685310317,
0.12197210341919, 0.121115745201095, 0.120880251090657, 0.120851770150267,
0.120746714650168, 0.120922991632831, 0.120866928018865), se_MEt_Irrigation = c(0.0790595725119853,
0.0819113174332981, 0.0818328749299557, 0.0834638025854297, 0.0818357384597404,
0.0830466544695816, 0.0830677796154873, 0.0829941906461297, 0.083141965909444,
0.082714324704666, 0.0809987066350066, 0.0810565659915952, 0.0792023249112186,
0.0779277210970589, 0.0797106575341609, 0.0796897823245035, 0.0793238667046254,
0.0794345101645159, 0.0805370559814554, 0.0816802765047257),
Irrigationtotal = c(3610, 3605, 3599, 3597, 3593, 3590, 3589,
3584, 3578, 3573, 3562, 3555, 3551, 3544, 3538, 3536, 3530,
3528, 3519, 3512)), .Names = c("MEt_R", "MEt_Irr", "se_MEt_Rainfed",
"se_MEt_Irrigation", "Irrigationtotal"), row.names = c(NA, 20L
), class = "data.frame")
折线图
out122$perc<-c(1:20)
l64<-ggplot(out122,aes(perc))
l65<-l64+geom_line(aes(y=MEt_R,colour="Rainfed"))+
geom_line(aes(y=MEt_R+se_MEt_Rainfed,colour="Rainfed range"))+
geom_line(aes(y=MEt_R-se_MEt_Rainfed,colour="Rainfed range"))+
geom_line(aes(y=MEt_Irr,colour="Irrigation"))+
geom_line(aes(y=MEt_Irr+se_MEt_Irrigation,colour="Irrigation range"))+
geom_line(aes(y=MEt_Irr-se_MEt_Irrigation,colour="Irrigation range"))+
scale_colour_manual(values=c("blue","light blue","green","light green"), name="")+
scale_x_discrete(name="Threshold irrigation (in percentage)",breaks=c(0, 250, 500,750,1000),
labels=c("0", "25", "50","75","100")) +
scale_y_continuous(name="MEt",limits = c(-0.20, 0.5))+
guides(fill=guide_legend(title=NULL))
l66<-l65+ theme_bw()+ggtitle("subsidies 1 large") +
theme(plot.title = element_text(lineheight=.8, face="bold"))
l66
条形图(这里我遇到了 x 轴的问题,它的排名不像 1、2、3...)
l1<- ggplot(data=out122, aes(x=rownames(out122), y=Irrigationtotal, fill=Irrigationtotal)) +
geom_bar(stat="identity")+ theme_bw()
l1
我找到了我自己问题的答案。我只是对图例有疑问(我将 post 放在新的 post 中)。
关于连续x轴的顺序问题,答案提供者:
out122$perc<- as.character(out221$perc)
out122$perc <- factor(out221$perc, levels=unique(foo.long$perc))
找到如何重叠两个图的问题的答案here。
获取组合图下的两个图例可以按照建议here完成。唯一我还不知道的是后面的传说怎么定位。
用户,
我有一个从 1 到 999 的连续 x 变量(我在下面提供了我的数据的 20 个观察样本以供使用)。
我想在同一个图上绘制一些折线图和条形图 such as this plot。我想在条形图上绘制的变量是变量 "Irrigationtotal".
我有两个问题: 1) 第一个问题是对于条形图,R 只接受 x 轴上的离散值。因此,它改变了我的连续值的顺序,例如: 1, 11, 101..., 2, 21, 22, ... 200, ... 3 33... 显然,我想要 1、2、3、4、5...
我意识到我可以使用 scale_x_discrete(限制= ...) 对于这个问题,但后来我想我将无法在与条形图相同的图上绘制折线图。
2) 第二个问题是,在共享相同的 x 轴时,我无法将它们很好地重叠在一起。
你可以在下面找到我分别为这两个图准备的代码。有人可以帮助解决上述问题吗?非常感谢!
数据
out122<- structure(list(MEt_R = c(-0.0541818151603231, -0.0562844791428272,
-0.0558715941992024, -0.0562399962945622, -0.0560460386125185,
-0.0570608897132082, -0.0569943385875705, -0.0568252787782472,
-0.0569942506473323, -0.0565197621205338, -0.056900534973487,
-0.0571427349989937, -0.0569618449465491, -0.0566601716889117,
-0.0563552308197707, -0.0568648464047371, -0.057047451157018,
-0.0571837090302319, -0.0588902340655496, -0.0592472918164029
), MEt_Irr = c(-0.0930626749780042, -0.0924059309460578, -0.0924771937440385,
-0.0905386156125412, -0.0914934037180768, -0.0898948119109486,
-0.0898827200499507, -0.090372707751177, -0.0901901622784647,
-0.0914484064620663, -0.0925147845884521, -0.0927733849042059,
-0.0960873954367445, -0.0948131376144847, -0.0955133693827158,
-0.0933133384990093, -0.0927340360155418, -0.0925138612415783,
-0.0896139882242573, -0.0912014136494108), se_MEt_Rainfed = c(0.124867384884912,
0.124157398945455, 0.124169568385358, 0.124110270348855, 0.12391954965997,
0.123742628011372, 0.123766054757713, 0.123576175335345, 0.12353428904291,
0.123443556846824, 0.122869340273675, 0.122594726299249, 0.122332685310317,
0.12197210341919, 0.121115745201095, 0.120880251090657, 0.120851770150267,
0.120746714650168, 0.120922991632831, 0.120866928018865), se_MEt_Irrigation = c(0.0790595725119853,
0.0819113174332981, 0.0818328749299557, 0.0834638025854297, 0.0818357384597404,
0.0830466544695816, 0.0830677796154873, 0.0829941906461297, 0.083141965909444,
0.082714324704666, 0.0809987066350066, 0.0810565659915952, 0.0792023249112186,
0.0779277210970589, 0.0797106575341609, 0.0796897823245035, 0.0793238667046254,
0.0794345101645159, 0.0805370559814554, 0.0816802765047257),
Irrigationtotal = c(3610, 3605, 3599, 3597, 3593, 3590, 3589,
3584, 3578, 3573, 3562, 3555, 3551, 3544, 3538, 3536, 3530,
3528, 3519, 3512)), .Names = c("MEt_R", "MEt_Irr", "se_MEt_Rainfed",
"se_MEt_Irrigation", "Irrigationtotal"), row.names = c(NA, 20L
), class = "data.frame")
折线图
out122$perc<-c(1:20)
l64<-ggplot(out122,aes(perc))
l65<-l64+geom_line(aes(y=MEt_R,colour="Rainfed"))+
geom_line(aes(y=MEt_R+se_MEt_Rainfed,colour="Rainfed range"))+
geom_line(aes(y=MEt_R-se_MEt_Rainfed,colour="Rainfed range"))+
geom_line(aes(y=MEt_Irr,colour="Irrigation"))+
geom_line(aes(y=MEt_Irr+se_MEt_Irrigation,colour="Irrigation range"))+
geom_line(aes(y=MEt_Irr-se_MEt_Irrigation,colour="Irrigation range"))+
scale_colour_manual(values=c("blue","light blue","green","light green"), name="")+
scale_x_discrete(name="Threshold irrigation (in percentage)",breaks=c(0, 250, 500,750,1000),
labels=c("0", "25", "50","75","100")) +
scale_y_continuous(name="MEt",limits = c(-0.20, 0.5))+
guides(fill=guide_legend(title=NULL))
l66<-l65+ theme_bw()+ggtitle("subsidies 1 large") +
theme(plot.title = element_text(lineheight=.8, face="bold"))
l66
条形图(这里我遇到了 x 轴的问题,它的排名不像 1、2、3...)
l1<- ggplot(data=out122, aes(x=rownames(out122), y=Irrigationtotal, fill=Irrigationtotal)) +
geom_bar(stat="identity")+ theme_bw()
l1
我找到了我自己问题的答案。我只是对图例有疑问(我将 post 放在新的 post 中)。
关于连续x轴的顺序问题,答案提供者:
out122$perc<- as.character(out221$perc)
out122$perc <- factor(out221$perc, levels=unique(foo.long$perc))
找到如何重叠两个图的问题的答案here。
获取组合图下的两个图例可以按照建议here完成。唯一我还不知道的是后面的传说怎么定位。