繁体   English   中英

更改条形图 ggplot2 中的内部图例顺序

[英]Change inside legend order in bargraph ggplot2

我想在我的条形图中将图例顺序更改为 q1、中位数、q3,因为现在首先命名的是介质,这并不合乎逻辑。

barmedium <- ggplot(data=medium, aes(x=year, y=budgetresidual, group=Legend)) +
  geom_line(aes(linetype=Legend))+
  geom_point() +
  scale_linetype_manual(values=c("solid", "longdash", "dotted")) +
  scale_x_continuous(breaks = round(seq(min(medium$year), max(medium$year), by = 1),1),) +
  scale_y_continuous(breaks =c(-15,-10,-5,0,5,10), limits = c(-16,10)) +
  labs( x = "Year", y = "Budget residual") + theme_bw()
barmedium <- barmedium  + theme_update(legend.position='top')
barmedium 
medium<- data.frame(
  stringsAsFactors = FALSE,
                            year = c(2012L,2012L,2012L,2013L,2013L,2013L,
                                     2014L,2014L,2014L,2015L,2015L,2015L,2016L,
                                     2016L,2016L,2017L,2017L,2017L,2018L,
                                     2018L,2018L,2019L,2019L,2019L),
                            Legend = c("q1","median","q3","q1","median","q3",
                                     "q1","median","q3","q1","median","q3",
                                     "q1","median","q3","q1","median","q3",
                                     "q1","median","q3","q1","median","q3"),
                  budgetresidual = c(-8,-1,4,-9,-4,3,
                                     -15,-9,1,-9,-3,
                                     3,-12,-5,-0,-10,
                                     -7,-2,0.2,3,8,-1,
                                     3,6)
             )

代表 package (v0.3.0) 于 2020 年 7 月 1 日创建

尝试将 Legend 列转换为您希望的因子重新排序类别:

medium<- data.frame(
  stringsAsFactors = FALSE,
  year = c(2012L,2012L,2012L,2013L,2013L,2013L,
           2014L,2014L,2014L,2015L,2015L,2015L,2016L,
           2016L,2016L,2017L,2017L,2017L,2018L,
           2018L,2018L,2019L,2019L,2019L),
  Legend = c("q1","median","q3","q1","median","q3",
             "q1","median","q3","q1","median","q3",
             "q1","median","q3","q1","median","q3",
             "q1","median","q3","q1","median","q3"),
  budgetresidual = c(-8,-1,4,-9,-4,3,
                     -15,-9,1,-9,-3,
                     3,-12,-5,-0,-10,
                     -7,-2,0.2,3,8,-1,
                     3,6)
)

medium$Legend  <- factor(medium$Legend, levels = c("q1", "median", "q3"))
barmedium <- ggplot(data=medium, aes(x=year, y=budgetresidual, group=Legend)) +
  geom_line(aes(linetype=Legend))+
  geom_point() +
  scale_linetype_manual(values=c("solid", "longdash", "dotted")) +
  scale_x_continuous(breaks = round(seq(min(medium$year), max(medium$year), by = 1),1),) +
  scale_y_continuous(breaks =c(-15,-10,-5,0,5,10), limits = c(-16,10)) +
  labs( x = "Year", y = "Budget residual") + theme_bw()
barmedium <- barmedium  + theme_update(legend.position='top')
barmedium 

暂无
暂无

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

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