ggplot2 facet_grid重复barplot名称

时间:2017-02-13 18:44:41

标签: r ggplot2

我尝试使用withReattempt创建条形图网格。我的数据如下:

facet_grid

我想为> data get mean catvar calcvar 1: Mexican American 6.7423680 RIDRETH1 CIGARETTES 2: other Hispanic 9.3955082 RIDRETH1 CIGARETTES 3: non-Hispanic White 15.9486910 RIDRETH1 CIGARETTES 4: non-Hispanic Black 10.2582135 RIDRETH1 CIGARETTES 5: other non-Hispanic 11.7396250 RIDRETH1 CIGARETTES 6: Mexican American 0.5812325 RIDRETH1 WHR 7: other Hispanic 0.5688999 RIDRETH1 WHR 8: non-Hispanic White 0.5587113 RIDRETH1 WHR 9: non-Hispanic Black 0.5614028 RIDRETH1 WHR 10: other non-Hispanic 0.5581675 RIDRETH1 WHR 11: Mexican American 28.5600862 RIDRETH1 BMXBMI 12: other Hispanic 27.8138462 RIDRETH1 BMXBMI 13: non-Hispanic White 27.0137246 RIDRETH1 BMXBMI 14: non-Hispanic Black 28.5602261 RIDRETH1 BMXBMI 15: other non-Hispanic 27.1031377 RIDRETH1 BMXBMI 16: No High School 14.9154844 DMDEDUC2 CIGARETTES 17: High School, No Diploma 15.5943692 DMDEDUC2 CIGARETTES 18: HS Diploma or Equivalent 15.5258469 DMDEDUC2 CIGARETTES 19: Some college/AA degree 13.6358341 DMDEDUC2 CIGARETTES 20: College graduate or above 11.7456132 DMDEDUC2 CIGARETTES 21: No High School 27.3320919 DMDEDUC2 BMXBMI 22: High School, No Diploma 27.4109358 DMDEDUC2 BMXBMI 23: HS Diploma or Equivalent 27.7410707 DMDEDUC2 BMXBMI 24: Some college/AA degree 27.6298412 DMDEDUC2 BMXBMI 25: College graduate or above 26.7682926 DMDEDUC2 BMXBMI 26: No High School 0.5774948 DMDEDUC2 WHR 27: High School, No Diploma 0.5662341 DMDEDUC2 WHR 28: HS Diploma or Equivalent 0.5669633 DMDEDUC2 WHR 29: Some college/AA degree 0.5639211 DMDEDUC2 WHR 30: College graduate or above 0.5479236 DMDEDUC2 WHR get mean catvar calcvar - catvar(例如,RIDRETH1与CIGARETTES,RIDRETH1与WHR)的每个组合生成一个条形图,并为calcvar中的每个条目添加一个条形。

例如,这是(一个丑陋的想法)我想要的get:RIDRETH1与CIGARETTES,WHR和BMXBMI:

enter image description here

catvar

但是,如果我尝试使用两个ggplot(data=data[data$catvar=="RIDRETH1"], aes(x=get, y=mean)) + facet_grid(calcvar ~ catvar, scales = "free_y") + geom_bar(stat="identity") + theme(axis.text.x = element_text(angle = 90)) (即同时显示RIDRETH1 DMDEDUC2),整个事情就会变成地狱:

enter image description here

catvar

这里是代码和基本的ggplot:

ggplot(data=data[data$catvar=="RIDRETH1"], aes(x=get, y=mean)) +
    facet_grid(calcvar ~ catvar, scales = "free_y") + 
    geom_bar(stat="identity") +
    theme(axis.text.x = element_text(angle = 90))

有关如何让data=structure(list(get = c("Mexican American", "other Hispanic", "non-Hispanic White", "non-Hispanic Black", "other non-Hispanic", "Mexican American", "other Hispanic", "non-Hispanic White", "non-Hispanic Black", "other non-Hispanic", "Mexican American", "other Hispanic", "non-Hispanic White", "non-Hispanic Black", "other non-Hispanic", "No High School", "High School, No Diploma", "HS Diploma or Equivalent", "Some college/AA degree", "College graduate or above", "No High School", "High School, No Diploma", "HS Diploma or Equivalent", "Some college/AA degree", "College graduate or above", "No High School", "High School, No Diploma", "HS Diploma or Equivalent", "Some college/AA degree", "College graduate or above"), mean = c(6.74236799891251, 9.39550816502028, 15.9486910291741, 10.2582134840889, 11.7396250210344, 0.581232525625264, 0.568899901864229, 0.558711316230234, 0.561402759873007, 0.558167495000229, 28.5600862089255, 27.8138462232903, 27.0137245744966, 28.5602261242299, 27.1031376882496, 14.9154843636288, 15.5943692351338, 15.5258469143284, 13.6358341183803, 11.7456132495542, 27.3320918541377, 27.4109358360509, 27.7410707468688, 27.6298412194686, 26.7682926409561, 0.577494827685547, 0.566234093777988, 0.566963343644022, 0.563921078038311, 0.547923552981604), catvar = c("RIDRETH1", "RIDRETH1", "RIDRETH1", "RIDRETH1", "RIDRETH1", "RIDRETH1", "RIDRETH1", "RIDRETH1", "RIDRETH1", "RIDRETH1", "RIDRETH1", "RIDRETH1", "RIDRETH1", "RIDRETH1", "RIDRETH1", "DMDEDUC2", "DMDEDUC2", "DMDEDUC2", "DMDEDUC2", "DMDEDUC2", "DMDEDUC2", "DMDEDUC2", "DMDEDUC2", "DMDEDUC2", "DMDEDUC2", "DMDEDUC2", "DMDEDUC2", "DMDEDUC2", "DMDEDUC2", "DMDEDUC2"), calcvar = c("CIGARETTES", "CIGARETTES", "CIGARETTES", "CIGARETTES", "CIGARETTES", "WHR", "WHR", "WHR", "WHR", "WHR", "BMXBMI", "BMXBMI", "BMXBMI", "BMXBMI", "BMXBMI", "CIGARETTES", "CIGARETTES", "CIGARETTES", "CIGARETTES", "CIGARETTES", "BMXBMI", "BMXBMI", "BMXBMI", "BMXBMI", "BMXBMI", "WHR", "WHR", "WHR", "WHR", "WHR")), .Names = c("get", "mean", "catvar", "calcvar"), row.names = c(NA, -30L), class = c("data.table", "data.frame")) ggplot(data=data, aes(x=get, y=mean)) + facet_grid(calcvar ~ catvar, scales = "free_y") + geom_bar(stat="identity") + theme(axis.text.x = element_text(angle = 90)) 不重复条形码的任何建议?

0 个答案:

没有答案