ggplot2:在散点图中反转离散类别在y轴上的顺序 [英] ggplot2: Reversing the order of discrete categories on y-axis in scatterplot
问题描述
我一直在试图弄清楚如何修改此代码,以便在散点图中,架构位于Y轴的顶部,视觉艺术位于Y轴的底部。
UPDATE:关于重复错误分类 - 我的观点是如果在ggplot2中有这样一个优雅的方法,而不是简单地重新排序数据框中的级别。
ggplot()+ geom_point(aes(x = Year,y = Subclass_Name,color = Subclass_Name,size = Transactions) = art_data,alpha = 0.7)
注意:有一个类似的问题回答了一个线图,但我无法使其适用于散点图:
I'm stuck trying to figure out how to revise this code so that in the scatterplot Architecture is on the top of the Y-axis and Visual Arts is on the bottom of the Y-axis.
UPDATE: With respect to the "duplicate" misclassification - My point was if there was an elegant way to do this WITHIN ggplot2, not simply reordering the levels in the data frame.
ggplot() + geom_point(aes(x = Year, y = Subclass_Name, colour = Subclass_Name, size = Transactions), data = art_data, alpha = 0.7)
Note: there was a similar question answered for a line plot but I can't make it work for the scatterplot: r - reverse order of discrete y axis in ggplot2
You can reverse the order of the levels of your Subclass_name variable.
## Some sample data
art_data <- data.frame(Subclass_Name=sample(c("Architecture", "Painting", "Visual Arts"), 100, rep=T),
Year=sample(1920:2015, 100, rep=T, prob=sort(rexp(96, 1/8))),
Transactions=sample(1:7, 100, rep=T))
## Initial levels
levels(art_data$Subclass_Name)
# [1] "Architecture" "Painting" "Visual Arts"
## Reverse the order of Subclass_Name levels
art_data$Subclass_Name <- factor(art_data$Subclass_Name,
levels=rev(levels(art_data$Subclass_Name)))
levels(art_data$Subclass_Name)
# [1] "Visual Arts" "Painting" "Architecture"
## Then make the plot
ggplot(art_data, aes(Year, Subclass_Name, color=Subclass_Name, size=Transactions)) +
geom_point(alpha=0.7) +
scale_color_discrete(breaks=rev(levels(art_data$Subclass_Name)),
labels=rev(levels(art_data$Subclass_Name))) # reverse labels in legend
这篇关于ggplot2:在散点图中反转离散类别在y轴上的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!