ggplot2条形图中的有序因子

发布于 2024-09-16 22:59:57 字数 742 浏览 2 评论 0原文

我有一个数据框,其中包含(为了简化)评委、电影和评分(评分按 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 计算机上有效。)如下所示: alt text

我尝试过,

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:
alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城 2024-09-23 22:59:57

我不确定您的示例数据框是否代表您发布的图像。您提到您的评级为 1-5 级,但您的图像显示为 -3 到 3 级。话虽如此,我认为这应该让您朝着正确的方向前进:

示例数据:

d = data.frame(judge=sample(c("alice","bob","tony"), 100, replace = TRUE)
    , movie=sample(c("toy story", "inception", "a league of their own"), 100, replace = TRUE)
    , rating =  sample(1:5, 100, replace = TRUE))

您与此最接近:

ggplot(d, aes(rating)) + geom_bar()

通过调整 geom_bar 中的默认 binwidth,我们可以使条形宽度更合适并处理评级作为一个因素将它们集中在标签上:

ggplot(d, aes(x = factor(rating))) + geom_bar(binwidth = 1)

alt text

如果您想在图表中合并其他变量之一,例如作为电影,您可以使用填充:

ggplot(d, aes(x = factor(rating), fill = factor(movie))) + geom_bar(binwidth = 1)

alt text

将电影放在 x 轴上并填充可能更有意义如果您要比较的电影数量较少,请使用评级:

ggplot(d, aes(x = factor(movie), fill = factor(rating))) + geom_bar(binwidth = 1)

如果这不能让您上路,请提供一个更具代表性的数据集示例。我无法重现排序问题,但这可能是由于您发布的示例数据与您正在分析的数据存在差异所致。

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:

d = data.frame(judge=sample(c("alice","bob","tony"), 100, replace = TRUE)
    , movie=sample(c("toy story", "inception", "a league of their own"), 100, replace = TRUE)
    , rating =  sample(1:5, 100, replace = TRUE))

You were closest with this:

ggplot(d, aes(rating)) + geom_bar()

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:

ggplot(d, aes(x = factor(rating))) + geom_bar(binwidth = 1)

alt text

If you wanted to incorporate one of the other variables in the chart such as the movie, you can use fill:

ggplot(d, aes(x = factor(rating), fill = factor(movie))) + geom_bar(binwidth = 1)

alt text

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:

ggplot(d, aes(x = factor(movie), fill = factor(rating))) + geom_bar(binwidth = 1)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文