r - Change order of elements in slopegraphs, ggplot
问题描述
I am trying to change the order of elements plotted in the slopegraph(below) generated in ggplot 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.
解决方案
一种可能的选择是df$type
在绘图之前更改类别并设置顺序。像这样:
df$type <- factor(df$type, levels = c('State', 'Regional Mean','National Mean')
然后执行您已有的 ggplot 代码。
让我知道这是否有帮助!
推荐阅读
- wordpress - WordPress 和 Chromebook 的 CORS 问题
- python - 在Python中将位板中的位移动到左侧的问题
- php - 由于路由问题,链接到另一个页面不起作用?
- python - 使用另一个函数更改生成器 - Python 3.x
- authentication - 用户在 OpenId Connect (AAD) 之后通过身份验证,但无法找到访问令牌
- java - 使用 Maven 的 javadoc 有什么问题?
- typescript - 在 TypeScript 中将所有类型的键分配给类
- c# - 如何确保在读取 C# 中的文件,然后将其存储在数据库中后,如果我要重新读取它,会出现错误
- java - 获取错误消息“线程“主”java.util.InputMismatchException 中的异常”
- javascript - 如何在浏览器中正确加载类