本文介绍了使用ggplot2在多面点图内的因子顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图改变ggplot2中多面点阵图面的绘图顺序,但是我无法使其工作。这是我融化的数据集: > London.melt 国家medal.type count 1韩国黄金13 2意大利黄金8 3法国黄金11 4澳大利亚黄金7 5日本黄金7 6德国黄金11 7英国& N.爱尔兰黄金29 $ b $ 8俄罗斯联邦黄金24 9中国黄金38 10美国黄金46 11韩国白银8 12意大利白银9 13法国银11 14澳大利亚银16 $ b $ 15日本银14 $ b $ 16德国银19 17英国和英国N.爱尔兰白银17 18俄罗斯联邦白银26 19中国白银27 20美国白银29 21韩国青铜7 22意大利青铜11 23法国铜牌12 24澳大利亚铜牌12 25日本铜牌17 26德国铜牌14 27英国& N.爱尔兰铜牌19 28俄罗斯联邦铜牌32 29中国铜牌23 30美国铜牌29 以下是我的绘图命令: qplot(x = count,y = country, data = London.melt,geom =point,facets = medal.type〜。) 我得到的结果如下: 这些刻面本身以我想要的顺序出现在这个图中。但是,在每个方面,我想按数字排序。也就是说,对于每种类型的奖牌,我都希望获得这些奖牌数量最多的国家,等等。在没有方面(比如说我们只看金牌)时,我成功地使用过的程序是在因子上使用 reorder 函数,国家,按 count 排序,但在目前的例子中这不起作用。 我会非常感谢你可能有任何建议。 解决方案这里有一个解决方案,使用粘贴,自由缩放和一些重新标记功能 使每个国家独一无二 London.melt $ country_l q #使用原始名称重命名国家q + scale_y_discrete(Country,breaks = London.melt $ country_l,label = London.melt $ country) I'm trying to change the plotting order within facets of a faceted dotplot in ggplot2, but I can't get it to work. Here's my melted dataset:> London.melt country medal.type count1 South Korea gold 132 Italy gold 83 France gold 114 Australia gold 75 Japan gold 76 Germany gold 117 Great Britain & N. Ireland gold 298 Russian Federation gold 249 China gold 3810 United States gold 4611 South Korea silver 812 Italy silver 913 France silver 1114 Australia silver 1615 Japan silver 1416 Germany silver 1917 Great Britain & N. Ireland silver 1718 Russian Federation silver 2619 China silver 2720 United States silver 2921 South Korea bronze 722 Italy bronze 1123 France bronze 1224 Australia bronze 1225 Japan bronze 1726 Germany bronze 1427 Great Britain & N. Ireland bronze 1928 Russian Federation bronze 3229 China bronze 2330 United States bronze 29and here's my plot command:qplot(x = count, y = country, data = London.melt, geom = "point", facets = medal.type ~.)The result I get is as follows:The facets themselves appear in the order I want in this plot. Within each facet, however, I'd like to sort by count. That is, for each type of medal, I'd like the country that won the greatest number of those medals on top, and so on. The procedure I have used successfully when there are no facets (say we're only looking at gold medals) is to use the reorder function on the factor country, sorting by count but this doesn't work in the present example.I'd greatly appreciate any suggestions you might have. 解决方案 Here a solution using paste, free scales and some relabelinglibrary(ggplot2)London.melt$medal.type<-factor(London.melt$medal.type, levels = c("gold","silver","bronze"))# Make every country uniqueLondon.melt$country_l <- with(London.melt, paste(country, medal.type, sep = "_"))#Reorder the unique countrysq <- qplot(x = count, y = reorder(country_l, count), data = London.melt, geom = "point") + facet_grid(medal.type ~., scales = "free_y")# Rename the countries using the original namesq + scale_y_discrete("Country", breaks = London.melt$country_l, label = London.melt$country) 这篇关于使用ggplot2在多面点图内的因子顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 20:28