问题描述
我有一个数据框(用于简化评委,电影和评分)(评分是1星级到5星级):
d = data.frame(判断= c(alice,bob,alice),movie = c(玩具故事,开始,开始),评分= c (1,3,5))
我想创建一个条形图,其中x轴是如果我这样做的话
b
pre> ggplot(d,aes(评分))+ geom_bar()
这个效果很好,除了这些酒吧并没有集中在每个评级上,并且每个酒吧的宽度都不理想。
如果我做了
ggplot(d,aes(factor(rating)))+ geom_bar()
pre>
恒星数量的顺序会在x轴上混乱。 (在我的Mac上,至少;出于某种原因,默认顺序在Windows机器上工作。)这是它的样子:
alt =alt text>
> ggplot(d,aes(factor(rating,ordered = T,levels = -3:3)))+ geom_bar()
但这似乎没有帮助。
如何让我的条形图看起来像上图,但是正确的排序在X轴上?
解决方案我不确定您的示例数据框是否代表您放置的图像。你提到你的评分是1-5分,但你的图片显示-3到3分。这就是说,我认为这应该让你朝着正确的方向前进:
示例数据:
<$ p $ (c(alice,bob,tony),100,replace = TRUE)
,movie = sample(c( 玩具故事,初始,他们自己的联盟),100,替换= TRUE)
,rating = sample(1:5,100,replace = TRUE))
您最接近这一点:
ggplot(d,aes(rating))+ geom_bar()
binwidth在 geom_bar 中,我们可以使条宽更合适,并将评级作为一个因素集中在标签上:
ggplot(d,aes(x = factor(rating)))+ geom_bar(binwidth = 1)
如果您想在图表中加入其他变量之一(例如电影),您可以使用fill:
ggplot(d,aes(x = factor(rating),fill = factor(movie)))+ geom_bar(binwidth = 1)
如果要将少量电影进行比较,将电影放在x轴上并填充评分可能更有意义:(d,aes(x = factor(movie),fill = factor(rating)))+ geom_bar(binwidth = 1)
如果这样做不能让你顺其自然,请举出更具代表性的数据集示例。我无法重新创建订购问题,但这可能是由于您发布的样本数据和您正在分析的数据存在差异。
ggplot网站也是一个很好的参考:
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?
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)
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)
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
这篇关于在ggplot2条形图中排序因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!