根据ggplot中的变量重新排列x轴 [英] Rearrange x axis according to a variable in ggplot
问题描述
我试图用ggplot做一个数据框 plot_data
的barplot。在x轴上,我希望有 Duree_moy_trans
(行程持续时间)的持续时间(Duree_moy_trans)
。 y轴应该是 value
,并且条应该被 Categorie
着色。
我尝试使用 reorder()
命令x轴和 scale_x_discrete()
设置标签,但未能获得所需的结果。如何绘制该图?
这是迄今为止我尝试过的数据和代码:
plot_data = structure(list(Destination = structure(c(1L,2L,6L,8L,11L,
12L),.Label = c(ABL,ARP ,ATH,
BIE,BOU,BRE,BRU,BRI,
CHA,CHE,CHI,CHO,
DOU,EGL,EPI,ETA,ETR,GRA,
IVR,JUV,LAN,
LAR ,LES,LEA,LON,MARO,
MAS,MAS,ORL,PET,
PON,RUN, SAI,
SAM,SAG,SAV,
SER,VER,VIL,VIT
),class = ),Duree_moy_trans = c(15L,36L,28L,44L,
32L,9L),Categorie = c(3,3,3,3,3,3 ),value = c(1,
3,2,3,1.333333333333333,4)),.Names = c(Destination,Duree_moy_trans,
Categorie,value), row.names = c(NA,6L),class =data.frame)
library(ggplot2)
ggplot(plot_data,aes(reorder(Duree_moy_trans, - 值),y =值,
group =分类,填充= Categorie))+
geom_bar(stat =identity)+
scale_x_discrete(name =Destination,
labels = paste(plot_data $ Destination,(,plot_data $ Duree_moy_trans, ),sep =))+
theme(plot.title = element_text(size = 16,lineheight = 2,face =bold))+
scale_y_continuous(name =Pourcentage% ,limits = c(0,25))
从你的问题,我不确定你想要的订单是什么:你说你要按 Duree_moy_trans
来订购,然后用 -value
在 reorder()
中。在下面的例子中,我会假设你想要通过 Duree_moy_trans
来订购。
Destination(Duree_moy_trans)
),我认为在绘制之前设置数据中的标签会更容易。您可以同时重新排列因子: new_dest < - paste0(plot_data $目标,(,plot_data $ Duree_moy_trans,))
plot_data $ Destination< - reorder(new_dest,plot_data $ Duree_moy_trans)
然后你可以用
ggplot(plot_data,aes(x = Destination,y = value,fill = Categorie ))+
geom_bar(stat =identity)+
scale_y_continuous(name =Pourcentage%,limits = c(0,25))
请注意,您不需要需要 group = Categorie
。
I'm trying to do a barplot of a data frame plot_data
with ggplot. On the x-axis, I want to have Destination
ordered by Duree_moy_trans
(duration of the trip) with labels of the form "Duration (Duree_moy_trans)
". The y-axis should be value
and the bars should be coloured by Categorie
.
I have tried to use reorder()
to order the x-axis and scale_x_discrete()
to set the labels, but have failed to get the desired result. How can I draw this plot?
This is the data and the code that I have tried so far:
plot_data= structure(list(Destination = structure(c(1L, 2L, 6L, 8L, 11L,
12L), .Label = c("ABL", "ARP", "ATH",
"BIE", "BOU", "BRE", "BRU", "BRI",
"CHA", "CHE", "CHI", "CHO",
"DOU", "EGL", "EPI", "ETA", "ETR", "GRA",
"IVR", "JUV", "LAN",
"LAR", "LES", "LEA", "LON", "MARO",
"MAS", "MAS", "ORL", "PET",
"PON", "RUN", "SAI",
"SAM", "SAG", "SAV",
"SER", "VER", "VIL", "VIT"
), class = "factor"), Duree_moy_trans = c(15L, 36L, 28L, 44L,
32L, 9L), Categorie = c("3", "3", "3", "3", "3", "3"), value = c(1,
3, 2, 3, 1.33333333333333, 4)), .Names = c("Destination", "Duree_moy_trans",
"Categorie", "value"), row.names = c(NA, 6L), class = "data.frame")
library(ggplot2)
ggplot(plot_data, aes(reorder(Duree_moy_trans, -value), y = value,
group= Categorie, fill = Categorie)) +
geom_bar(stat = "identity") +
scale_x_discrete(name = "Destination",
labels = paste(plot_data$Destination," ( ",plot_data$Duree_moy_trans," ) ",sep="")) +
theme(plot.title = element_text(size=16,lineheight=2, face="bold")) +
scale_y_continuous(name=" Pourcentage %",limits = c(0,25))
From your question, I am not sure what the desired order is: you say you want to order by Duree_moy_trans
, but then use -value
in reorder()
. In the following, I will assume that you want to order by Duree_moy_trans
.
In order to have the labels you want (Destination (Duree_moy_trans)
), I think it is easier to set the labels in the data before you plot. You can reorder the factor at the same time:
new_dest <- paste0(plot_data$Destination, " (", plot_data$Duree_moy_trans, ")")
plot_data$Destination <- reorder(new_dest, plot_data$Duree_moy_trans)
And then you can plot with
ggplot(plot_data, aes(x = Destination, y = value, fill = Categorie)) +
geom_bar(stat = "identity") +
scale_y_continuous(name=" Pourcentage %",limits = c(0,25))
Note that you don't need group = Categorie
.
这篇关于根据ggplot中的变量重新排列x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!