简体   繁体   English

ggplot2在连续条形图上出现级别问题

[英]Trouble with levels on continuous bar chart with ggplot2

I have a dataset that looks something like this: 我有一个看起来像这样的数据集:

testSet <- data.table(date=as.Date(c("2013-07-02","2013-08-03","2013-09-04",
                                     "2013-10-05","2013-11-06")), 
                      yr = c(2013,2013,2013,2013,2013), 
                      mo = c(07,08,09,10,11), 
                      da = c(02,03,04,05,06), 
                      plant = LETTERS[1:5], 
                      product = letters[26:22], 
                      rating = runif(5))

What I would like to do is plot 2 graphs using ggplot2 . 我想做的是使用ggplot2绘制2个图形。

The first would give me a dodged, continuous bar chart for all months that would have the product on the x , the ratings on the y , and the dates grouped and plotted on their respective products. 第一个会给我一张所有月份的,连续的条形图,该图将在x上显示产品,在y上显示评级,并将日期分组并绘制在各自的产品上。

x = product

y = rating

Dodge = date

The second that I'm trying to create is a dodged, continuous bar chart for one month that would have the plant on the x, the ratings on the y, and the product grouped and plotted on their respective plants. 我要创建的第二个图表是一张连续的,连续的条形图,该图将x表示工厂,将y表示评级,并将产品分组并绘制在各自的工厂上。

x = plant

y = rating

Dodge = product

I'm looking for an output that is very similar to this: http://docs.ggplot2.org/0.9.3/geom_bar-28.png but continuous. 我正在寻找与以下内容非常相似的输出: http : //docs.ggplot2.org/0.9.3/geom_bar-28.png但连续。

I've had issues trying to figure out how the levels things works and haven't seen an example of a dodged, continuous chart. 我在尝试弄清各个级别的工作原理时遇到了问题,还没有看到一个躲避的连续图表的示例。

Here is the code I have created so far: 这是我到目前为止创建的代码:

testMean <- tapply(testSet$rating, list(testSet$mo), mean)

testLevels <- factor(levels(testSet$product,testSet$mo), 
                  levels = levels(testSet$product,testSet$mo))

qplot(testLevels, aes(testMean, fill=cut)) +
          geom_bar(position="dodge", stat="identity")

This is what the ggplot2 site says about creating a continuous bar chart, but it doesn't say anything about how to do it with multiple graphs overlayed on top of each other and then dodged, like in the one I linked to earlier. 这是ggplot2网站关于创建连续条形图的内容,但是它并没有说明如何将多个图形相互叠加然后躲避,如我先前链接的那样。 Here is their code: 这是他们的代码:

meanprice <- tapply(diamonds$price, diamonds$cut, mean)

cut <- factor(levels(diamonds$cut), levels = levels(diamonds$cut))
qplot(cut, meanprice)

I appreciate the help, guys! 谢谢大家的帮助!

I ended up using the diamonds built in data set to get my question answered. 我最终使用内置于数据集中的菱形来回答我的问题。 All the thanks in the world to @carloscinelli for his assistance. 世界上所有感谢@carloscinelli的帮助。

library(data.table)
data <- data.table(diamonds)[,list(mean_carat=mean(carat)), by=c('cut', 'color')]

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM