根据数据框的数值栏,在R中绘制“十大”风格的排行榜 [英] Plot a 'top 10' style list/ranking in R based on numerical column of dataframe

查看:101
本文介绍了根据数据框的数值栏,在R中绘制“十大”风格的排行榜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含字符串变量和数字变量的R数据帧,我想根据数值变量的值绘制前10个字符串。



我当然可以很简单地获得前十名的条目:

  top10_rank<  -  rank [order(rank $ numeric_var_name),] 

可视化这是为了简单地尝试绘制如下:

  ggplot(data = top10_rank,aes(x = top10_rank $ numeric_var_name,y = top10_rank $ string_name))+ geom_point(size = 3)

- 问题是,y轴上的字符串按字母顺序排序,而不是数值。



我的偏好是找到一种方法来绘制前10位字符串,而不用麻烦显示数字变量 - 基本上只是一个列表(更好的是,如果我可以枚举列表)。我试图绘制,所以看起来比简单的将文本转移到屏幕更令人愉快。



任何想法非常感谢!

解决方案

y轴刻度线可能按字母顺序排列,但是这些点按照top10_rank数据帧的顺序(从左到右)绘制。您需要做的是更改y轴的顺序。将它添加到您的ggplot调用中 + scale_y_discrete(limits = top10_rank $ String),它应该可以工作。

  ggplot(data = top10_rank,aes(x = top10_rank $ Number,
y = top10_rank $ String))+ geom_point(size = 3)+ scale_y_discrete(limits = top10_rank $ String)

这是一个关于R图形的很好的资源的链接: R Graphics Cookbook


I have an R dataframe that contains a string variable and a numerical variable, and I would like to plot the top 10 strings, based on the value of the numerical variable.

I can of course get the top 10 entries pretty simply:

top10_rank <- rank[order(rank$numerical_var_name),]

My first approach to trying to visualize this was to simple attempt to plot this like:

ggplot(data=top10_rank, aes(x = top10_rank$numerical_var_name, y = top10_rank$string_name)) + geom_point(size=3)

And to a first approximation this "works" - the problem is that the strings on the y axis are sorted alphabetically rather than by the numerical value.

My preference would be to find a way to plot the top 10 strings without having to bother showing the numerical variable at all - just basically as a list (even better would be if I could enumerate the list). I am attempting to plot this so it looks more pleasing than simply dumping the text to the screen.

Any ideas greatly appreciated!

解决方案

The y-axis tick marks may be sorted alphabetically, but the points are drawn in order(from left to right) of the top10_rank dataframe. What you need to do is change the order of the y-axis. Add this to your call of ggplot + scale_y_discrete(limits=top10_rank$String) and it should work.

ggplot(data=top10_rank, aes(x = top10_rank$Number, 
y = top10_rank$String)) + geom_point(size=3) + scale_y_discrete(limits=top10_rank$String)

Here is a link to a great resource on R graphics: R Graphics Cookbook

这篇关于根据数据框的数值栏,在R中绘制“十大”风格的排行榜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆