ggplot2条形图中的有序因子
我有一个数据框,其中包含(为了简化)评委、电影和评分(评分按 1 星到 5 星的等级排列):
d = data.frame(judge=c("alice","bob","alice"), movie=c("toy story", "inception", "inception"), rating=c(1,3,5))
我想创建一个条形图,其中 x 轴是星星的数量,每个条形都是该星星的评分数。
如果我
ggplot(d, aes(rating)) + geom_bar()
这样做,效果很好,除了条形图未在每个评级上居中并且每个条形图的宽度不理想之外。
如果我这样做,
ggplot(d, aes(factor(rating))) + geom_bar()
x 轴上星星数量的顺序就会混乱。 (至少在我的 Mac 上;出于某种原因,默认排序在 Windows 计算机上有效。)如下所示:
我尝试过,
ggplot(d, aes(factor(rating, ordered=T, levels=-3:3))) + geom_bar()
但这似乎没有帮助。
如何使我的条形图看起来如上图所示,但 X 轴上的顺序正确?
I have a data frame with (to simplify) judges, movies, and ratings (ratings are on a 1 star to 5 star scale):
d = data.frame(judge=c("alice","bob","alice"), movie=c("toy story", "inception", "inception"), rating=c(1,3,5))
I want to create a bar chart where the x-axis is the number of stars and the height of each bar is the number of ratings with that star.
If I do
ggplot(d, aes(rating)) + geom_bar()
this works fine, except that the bars aren't centered over each rating and the width of each bar isn't ideal.
If I do
ggplot(d, aes(factor(rating))) + geom_bar()
the order of the number of stars gets messed up on the x-axis. (On my Mac, at least; for some reason, the default ordering works on a Windows machine.) Here's what it looks like:
I tried
ggplot(d, aes(factor(rating, ordered=T, levels=-3:3))) + geom_bar()
but this doesn't seem to help.
How can I get my bar chart to look like the above picture, but with the correct ordering on the x-axis?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您的示例数据框是否代表您发布的图像。您提到您的评级为 1-5 级,但您的图像显示为 -3 到 3 级。话虽如此,我认为这应该让您朝着正确的方向前进:
示例数据:
您与此最接近:
通过调整
geom_bar
中的默认 binwidth,我们可以使条形宽度更合适并处理评级作为一个因素将它们集中在标签上:如果您想在图表中合并其他变量之一,例如作为电影,您可以使用填充:
将电影放在 x 轴上并填充可能更有意义如果您要比较的电影数量较少,请使用评级:
如果这不能让您上路,请提供一个更具代表性的数据集示例。我无法重现排序问题,但这可能是由于您发布的示例数据与您正在分析的数据存在差异所致。
ggplot 网站也是一个很好的参考: http://had.co.nz/ggplot2/geom_bar .html
I'm not sure your sample data frame is representative of the images you put up. You mentioned your ratings are on a 1-5 scale, but your images show a -3 to 3 scale. With that said, I think this should get you going in the right direction:
Sample data:
You were closest with this:
and by adjusting the default binwidth in
geom_bar
we can make the bar widths more appropriate and treating rating as a factor centers them over the label:If you wanted to incorporate one of the other variables in the chart such as the movie, you can use fill:
It may make more sense to put the movies on the x axis and fill with the rating if you have a small number of movies to compare:
If this doesn't get you on your way, put up a more representative example of your dataset. I wasn't able to recreate the ordering problems, but that could be due to a difference in the sample data you posted and the data you are analyzing.
The ggplot website is also a great reference: http://had.co.nz/ggplot2/geom_bar.html