使用数据集mpg,我能够绘制城市mpg,高速公路mpg和班级模型之间的三向关系。我使用以下代码(使用ggplot2)输出附件Boxplot.png中显示的图表。
ggplot(mpg,aes(cty,hwy))+
aes(color=class)+
geom_boxplot()+
facet_grid(.~class, scales='free')+
theme(axis.text.x = element_text(angle = -90, vjust = 1, hjust = 0))+
scale_x_continuous('City mpg')+
scale_y_continuous('Highway mpg')+
theme(legend.position="none")
问题:
我想按照增加中位数的顺序显示箱形图。我该怎么办?
我可以在每个箱线图的顶部显示中值吗?
答案 0 :(得分:2)
require(ggplot2)
aggregate(mpg$hwy, by=list(mpg$class), median)
mpg$class <- factor(mpg$class, levels = c("compact", "midsize", "subcompact",
"2seater", "minivan", "suv", "pickup"))
ggplot(mpg,aes(cty,hwy))+
aes(color=class)+
geom_boxplot()+
facet_grid(.~class, scales='free')+
theme(axis.text.x = element_text(angle = -90, vjust = 1, hjust = 0))+
scale_x_continuous('City mpg')+
scale_y_continuous('Highway mpg')+
theme(legend.position="none")
您还可以显示中位数。这是一个很好的解释/例子:
How to display the median value in a boxplot in ggplot?
示例是:
library(plyr)
library(ggplot2)
p_meds <- ddply(p, .(TYPE), summarise, med = median(TOTALREV))
ggplot(p,aes(x = TYPE, y = TOTALREV)) +
geom_boxplot() +
geom_text(data = p_meds, aes(x = TYPE, y = med, label = med),
size = 3, vjust = -1.5)
答案 1 :(得分:-1)
我经常做的是将 apply plugin: 'com.android.application' --> apply plugin: 'com.android.library'
变量设为一个因子,其中级别按您的意愿排序。这样,您的构面图就可以按照您的意愿进行排序。
要在顶部显示中值,我经常重新定义class
变量,例如class