首页 > 解决方案 > Change order of elements in slopegraphs, ggplot

问题描述

I am trying to change the order of elements plotted in the slopegraph(below) generated in ggplot enter image description here My data set is

State.Name      value      Challenging          type
 Alabama      0.03549384        Crop           State
 Alabama      0.15840594     Pasture           State
 Alabama      0.06373341        Crop    Regional Mean
 Alabama      0.18004195     Pasture    Regional Mean
 Alabama      0.06763161        Crop    National Mean
 Alabama      0.11543352     Pasture    National Mean

My code is

plot<-ggplot(data = above df, aes(x = type, y = value, group = Challenging, colour= Challenging)) +
geom_line(size = 1) +
geom_point(size = 1)+ theme(legend.title=element_blank())+ theme_minimal()+  theme(legend.title = element_blank())+
labs(title=statenames[[i]])+ scale_color_manual(values =c("indianred4","yellow4"))+ 
theme(axis.title.x=element_blank())+ ylab("Opportunity in Challenging Soil Conditions (Mha)")

How do I edit above code such that the order of plotting in State, Regional Mean, National Mean instead of what the plot currently is i.e. National Mean, Regional Mean and State.

标签: rggplot2

解决方案


一种可能的选择是df$type在绘图之前更改类别并设置顺序。像这样:

df$type <- factor(df$type, levels = c('State', 'Regional Mean','National Mean')

然后执行您已有的 ggplot 代码。

让我知道这是否有帮助!


推荐阅读