生成堆积条形图,对应于ggplot

时间:2015-07-07 11:48:39

标签: r ggplot2 aggregate bar-chart

我有以下数据集:

require(ggplot2)         # To do the graphs
require(plyr)            # To join data frames in he list
require(stringr)         # To clean strings
require(reshape2)        # To melt the data
require(scales)          # To handle percentage formats
require(grid)            # To handle units

set.seed(1)
df <- data.frame(group=c("Group A", "Group A", "Group A", "Group A", "Group B","Group B","Group B", "Group B", "Group B", "Group B", "Group B", "Group C", "Group C", "Group C", "Group C", "Group C", "Group C", "Group C", "Group C"), observation=c("OBS00001", "OBS00002", "OBS00003", "OBS00004", "OBS00005", "OBS00006", "OBS00007", "OBS00008", "OBS00009", "OBS00010", "OBS00011", "OBS00012", "OBS00013", "OBS00014", "OBS00015", "OBS00016", "OBS00017", "OBS00018", "OBS00019"), 
                 important_value = sample(1:3, 19, replace=T),
                 second_value = runif(n = 19),
                 some_random_stuff = runif(n = 19),
                 other_indicator = runif(n = 19))

我想生成以下图:

sample bar chart

理想情况下,我想要的是情节:

  • 提供从important_value
  • 中的值计数得出的百分比堆积条形
  • 使用与另一个任意值对应的点叠加条形图,在本例中为second_value,并将放置在条形图上,但在单独的y轴上提供值

我的初始代码如下:

# Melt the data frame
df_mlt <- melt(data = df[,c("observation", "group","important_value")],
                    id.vars = c("observation", "group"))
# Sort for the chart
df_mlt <- df_mlt[order(df_mlt$value, df_mlt$group),]

# Get average for the population density
df_avg <- aggregate(x = df$second_value, by = list(df$group),
                         FUN = mean, na.rm = TRUE)
# Graph
ggplot(df_mlt, aes(x = group, y = value, fill = factor(value))) +
  geom_bar(stat = "identity", position = "fill") +
  scale_y_continuous(labels = percent_format()) +
  geom_point(data = df_avg, aes(x = as.numeric(Group.1), y = x)) +
  ggtitle("Some title") +
  theme(plot.title = element_text(lineheight = .8, face = "bold"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        panel.margin = unit(c(0, 0, 0, 0), "cm"),
        plot.margin = unit(c(0, 0, 0, 0), "cm"),
        axis.line = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        legend.key = element_rect(linetype = 'blank'))

自然,尝试运行此代码将无法正常工作。在此特定示例的上下文中,错误是:

  

因子(值)出错:未找到对象'值'

修改

根据以下评论的要求,我添加了我使用的软件包。 通过解释的方式,一些包与下面的代码没有直接关系。我正在使用它们对我使用的原始数据做一些转换。为了以防万一,我决定列出完整的清单。例如,包stringr在这里不应该相关。

编辑2

这个特殊错误似乎与:

有关
scale_y_continuous(labels = percent_format()) +

应用于使用多个数据集的图形定义时。因此,最后一个问题是:如何强制scale_y...使用原始数据,并在示例中定义剩余的图。

0 个答案:

没有答案