使用ggplot时,保持与数据文件中的顺序相同 [英] Keep same order as in data files when using ggplot

查看:478
本文介绍了使用ggplot时,保持与数据文件中的顺序相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面附加的数据来制作boxplot。
数据链路
https://www.dropbox.com/s/ dt1nxnkhq90nea4 / GTAP_Sims.csv



到目前为止,我已经使用了以下代码:



<$

evBASE.f< - 子集(ccwelfrsts,tradlib ==BASE)
p #在BASE场景下为所有区域分配EV < - ggplot(data = evBASE.f,aes(factor(region),ev))
p + geom_boxplot()+
theme(axis.text.x = element_text(color ='black', angle = 90,size = 16))+
theme(axis.text.y = element_text(color ='black',size = 16))

它重现了以下情节:
Plot
file:/// C:/Users/iouraich/Documents/ggplot_Results.htm



我在这里查找的是让plot中的x轴与csv文件中的标题region的顺序匹配。



ggplot中有任何可以控制的选项吗?



非常感谢

解决方案

基本上你只需要 region < - factor(region,levels =独特(区域))来指定它们在数据中出现的顺序。



基于数据的完整解决方案您提供了:

  ccwelfrsts<  -  read.csv(GTAP_Sims.csv)
## unmangle data
ccwelfrsts [5:8]< - sapply(ccwelfrsts [5:8],as.numeric)
evBASE.f< - droplevels(子集(ccwelfrsts,tradlib == 碱基))
## reorder region levels
evBASE.f < - transform(evBASE.f,region = factor(region,levels = unique(region)))
library(ggplot2)
theme_set(theme_bw())
p < - ggplot(data = evBASE.f,aes(region,ev))
p + geom_boxplot()+
theme(axis.text.x = element_text (color ='black',angle = 90,size = 16))+
theme(axis.text.y = element_text(color ='black',size = 16))+
xlab )

您可能会考虑switc (通过 coord_flip 或通过显式切换x和y轴映射)来使标签更容易阅读,尽管在y上使用数字响应的布局轴对大多数观众来说更为熟悉。

I am using the attached data below to produce boxplot. Datalink https://www.dropbox.com/s/dt1nxnkhq90nea4/GTAP_Sims.csv

So far, I have this code that I am using:

# Distribution of EV for all regions under the BASE scenario

evBASE.f <- subset(ccwelfrsts, tradlib =="BASE")
p <- ggplot(data = evBASE.f, aes(factor(region), ev))
p + geom_boxplot() + 
    theme(axis.text.x = element_text(colour = 'black', angle = 90, size = 16)) +
    theme(axis.text.y = element_text(colour = 'black', size = 16))

it reproduces a plot that looks: Plot file:///C:/Users/iouraich/Documents/ggplot_Results.htm

What I am looking for here is to have the x-axis in the plot match the order of the header "region" in the csv file.

Is there any option within ggplot that allows to control for that?

Thanks a lot

解决方案

Basically you just need region <- factor(region,levels=unique(region)) to specify the levels in the order in which they appear in the data.

A full solution based on the data you provided:

ccwelfrsts <- read.csv("GTAP_Sims.csv")
## unmangle data
ccwelfrsts[5:8] <- sapply(ccwelfrsts[5:8],as.numeric)
evBASE.f <- droplevels(subset(ccwelfrsts, tradlib =="BASE"))
## reorder region levels
evBASE.f <- transform(evBASE.f,region=factor(region,levels=unique(region)))
library(ggplot2)
theme_set(theme_bw())
p <- ggplot(data = evBASE.f, aes(region, ev))
p + geom_boxplot() + 
    theme(axis.text.x = element_text(colour = 'black', angle = 90, size = 16)) +
    theme(axis.text.y = element_text(colour = 'black', size = 16))+
    xlab("")

You might consider switching the orientation of the graph (via coord_flip or by explicitly switching x and y axis mappings) to make the labels easier to read, although the layout with the numerical response on the y axis is more familiar to most viewers.

这篇关于使用ggplot时,保持与数据文件中的顺序相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆