本文介绍了ggplot2:在自由标度上反转每个方面的离散字符变量的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想绘制一个刻面的ggplot2点图。 x轴是连续的,y轴是动物的列表。根据饮食行为绘制并刻画两个变量。 y轴是免费的,因为每只动物只出现在一个饮食行为类别中。 $首先清理数据集: msleep.noNA< - msleep [!is.na(msleep $ vore) ,] msleep.noNA.red< - msleep.noNA [c(1,3,6,7)] msleep.noNA.red< - msleep.noNA.red [!is。 na(msleep.noNA.red [4]),] msleep.noNA.red< - melt(msleep.noNA.red) pg< - ggplot(msleep.noNA .red,aes(value,name,color = variable))+ geom_point()+ facet_grid(vore〜。,scale =free_y,space =free_y) pg #尝试颠倒y轴的顺序: pg + scale_y_reverse() #不可能,因为它的一个因素,但它不被归类为: class(msleep.noNA.red $ name) 有没有人有一些线索如何在每个子图中使动物名称按字母顺序排列?解决方案 You ca n将字符串向量转换为因子并指定级别的顺序: 以下命令将创建一个因子。级别按字母顺序递减: msleep.noNA.red< - within(msleep.noNA.red, name< - order(name,levels = rev(sort(unique(name))))) 现在您可以绘制数据: pg geom_point()+ facet_grid(vore〜。,scale =free_y,space =free_y) I'd like to plot a faceted ggplot2 dot plot. x-axis is continuous, y-axis is a list of animals. Two variables are plotted and faceted according to eating behavior. The y-axis is on a free scale because each animal only appears in one eating behavior category.library(ggplot2)# First clean up the data set:msleep.noNA <- msleep[!is.na(msleep$vore),]msleep.noNA.red <- msleep.noNA[c(1,3,6,7)]msleep.noNA.red <- msleep.noNA.red[!is.na(msleep.noNA.red[4]),]msleep.noNA.red <- melt(msleep.noNA.red)pg <- ggplot(msleep.noNA.red, aes(value, name, colour = variable)) + geom_point() + facet_grid(vore ~ ., scale="free_y", space = "free_y")pg# Try to reverse order of the y axis:pg + scale_y_reverse()# Not possible because its a factor, but it's not classified as such:class(msleep.noNA.red$name)Does anybody have some clues as to how I can make the list of animal names alphabetical in each sub-plot? 解决方案 You can transform the string vector to a factor and specify the order of levels:The following command will create a factor. The levels are in decreasing alphabetical order:msleep.noNA.red <- within(msleep.noNA.red, name <- ordered(name, levels = rev(sort(unique(name)))))Now you can plot the data:pg <- ggplot(msleep.noNA.red, aes(value, name, colour = variable)) + geom_point() + facet_grid(vore ~ ., scale="free_y", space = "free_y") 这篇关于ggplot2:在自由标度上反转每个方面的离散字符变量的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 00:33