English 中文(简体)
图2
原标题:ordered factors in ggplot2 bar chart
  • 时间:2010-09-01 21:52:26
  •  标签:
  • r
  • 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()

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

审判

ggplot(d, aes(factor(rating, ordered=T, levels=-3:3))) + geom_bar()

但这似乎无济于事。

我怎么能够看到我的条 chart,看上去看上去,但是在X-轴上的正确次序?

最佳回答

我不相信你的抽样数据框架代表了你所树立的图像。 你提到你的评级为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中调整违约的双维线,我们可以使条码更为合适,并将评级作为标签上的一个因素中心处理:

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

“alt

如果你想要将其他变量之一纳入电影等图表,你可以填写:

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

“alt

如果你有少数电影比较:

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

如果你不这样做,就树立了你数据集的更具有代表性的例子。 我没有能够重新研究定购问题,但这可能是你所公布抽样数据和你分析的数据不同所致。

http://had.co.nz/ggplot2/geom_bar.html

问题回答

暂无回答




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签