在分类X变量(数字类)上按组添加geom_density

时间:2015-12-30 18:24:46

标签: r ggplot2

我是r的新手,希望能帮助我尝试将一条线叠加到我在ggplot中制作的分组条形图上。我的初始绘图是从我创建的表(PlotData3)生成的,该表包含三列,我分配给x值,分组和y值,图表如下:

PlotData3 <- structure(list(AllCanc.Risk.Group = c(">200", "10-19", "100-199",
"20-29", "30-39", "40-49", "50-59", "60-69", "70-79", "80-89",
"90-99", ">200", "10-19", "100-199", "20-29", "30-39", "40-49",
"50-59", "60-69", "70-79", "80-89", "90-99", ">200", "10-19",
"100-199", "20-29", "30-39", "40-49", "50-59", "60-69", "70-79",
"80-89", "90-99"), variable = c("US", "US", "US", "US", "US",
"US", "US", "US", "US", "US", "US", "AT", "AT", "AT", "AT", "AT",
"AT", "AT", "AT", "AT", "AT", "AT", "SW", "SW", "SW", "SW", "SW",
"SW", "SW", "SW", "SW", "SW", "SW"), value = c(0.00664835861606374,
2.44355372567952, 0.130822020220507, 15.4709366277295, 32.4518264644337,
32.2667953215994, 13.3192561678316, 2.7733516677064, 0.675660976523301,
0.332458575790359, 0.128690093869533, 0.211223625656804, 1e-06,
2.43781818419616, 1e-06, 4.09352040466, 28.1323875136102, 37.2534225747702,
19.8154572533735, 4.55373287077757, 1.58548507865301, 1.91695249430252,
1e-06, 1e-06, 1e-06, 23.3036724323788, 52.5797733223538, 20.7005565950558,
2.56960027700278, 0.522043694524534, 0.177713854060081, 1e-06,
0.146639824624097)), row.names = c(NA, -33L), .Names = c("AllCanc.Risk.Group",
"variable", "value"), class = "data.frame")

library(ggplot2)
fill_palette <-c("darkorange", "dimgray", "dodgerblue")
ggplot(PlotData3, aes(x=factor(AllCanc.Risk.Group), y=value)) +   ###send data to ggplot
  geom_rect(data=PlotData3, aes(xmin=-Inf, xmax=1.5, ymin=-Inf, ymax=+Inf), fill='gray80', alpha=0.02)+    ##Create background to help visualiztion
  geom_rect(data=PlotData3, aes(xmin=1.5, xmax=2.5, ymin=-Inf, ymax=+Inf), fill='white', alpha=0.02)+
  geom_rect(data=PlotData3, aes(xmin=2.5, xmax=3.5, ymin=-Inf, ymax=+Inf), fill='gray80', alpha=0.02)+
  geom_rect(data=PlotData3, aes(xmin=3.5, xmax=4.5, ymin=-Inf, ymax=+Inf), fill='white', alpha=0.02)+
  geom_rect(data=PlotData3, aes(xmin=4.5, xmax=5.5, ymin=-Inf, ymax=+Inf), fill='gray80', alpha=0.02)+
  geom_rect(data=PlotData3, aes(xmin=5.5, xmax=6.5, ymin=-Inf, ymax=+Inf), fill='white', alpha=0.02)+
  geom_rect(data=PlotData3, aes(xmin=6.5, xmax=7.5, ymin=-Inf, ymax=+Inf), fill='gray80', alpha=0.02)+
  geom_rect(data=PlotData3, aes(xmin=7.5, xmax=8.5, ymin=-Inf, ymax=+Inf), fill='white', alpha=0.02)+
  geom_rect(data=PlotData3, aes(xmin=8.5, xmax=9.5, ymin=-Inf, ymax=+Inf), fill='gray80', alpha=0.02)+
  geom_rect(data=PlotData3, aes(xmin=9.5, xmax=10.5, ymin=-Inf, ymax=+Inf), fill='white', alpha=0.02)+
  geom_rect(data=PlotData3, aes(xmin=10.5, xmax=11.5, ymin=-Inf, ymax=+Inf), fill='gray80', alpha=0.02)+
  geom_bar(position="dodge", stat="identity",aes(fill=factor(variable))) +     # create bar chart, dodge for a clustered chart
  coord_cartesian(ylim=c(0, 60)) +     # axis limits
  xlab("All Cancer Risk Group\n (per million)") + ylab("Percent of Residents") +  # axis lables
  scale_fill_manual(name="", values=fill_palette) +  #legend title, fill
  theme(axis.text.x = element_text(size=8))+    #axis font
  scale_x_discrete(limits=c("<10", "10-19", "20-29", "30-39", "40-49", "50-59", "60-69", "70-79","80-89", "90-99","100-199",">200"))+  #order the x label correctly
  theme(axis.text.x=element_text(angle=-90))+   #adjust the x lable to vertical
  theme(axis.title=element_text(size=10))+    #adjust the axis/legend fonts
  theme(legend.text=element_text(size=10))+
  theme(panel.background = element_blank())+
  geom_density(data = PlotData3, x = factor(AllCanc.Risk.Group), y = value, fill = variable, position = stack)

我可以生成结果图表

enter image description here

我想叠加密度线以更好地可视化两组之间分布的差异。我尝试了不同的geom_density()组合,但一直得到同样的错误:

Error in eval(expr, envir, enclos) : object 'y' not found

我错过了什么吗?

这是我的数据表

我最接近的是添加:

geom_density(stat="identity", fill=NA)

哪个没有错误但给了我一个看起来像这样的结果(垂直线条) enter image description here

我也尝试过:

 geom_density(data=PlotData3, aes(x=AllCanc.Risk.Group, group=variable, fill=NA))

geom_density(data=PlotData3, aes(x=AllCanc.Risk.Group, y=value, group=variable, fill=NA))

哪个给了

  

eval(expr,envir,enclos)中的错误:找不到对象'y'

1 个答案:

答案 0 :(得分:0)

您的问题是fill = NA。您目前正选择填写不存在的内容。而是选择使用包含variableATSW的{​​{1}}填充它。

相反,请使用US

还可以考虑添加一些geom_density(aes(x = AllCanc.Risk.Group, y = value, fill = factor(variable), position = "stack", stat = "identity"))来使密度图的不同图层透视。