ggplot2:如何设置特定条的条宽度?

时间:2018-06-20 09:24:51

标签: r ggplot2 bar-chart

我正在使用ggplot2绘制一组数值。在数据集中的变量中,有两个未完全交叉的变量。这两个变量是身高(4个级别:高,紧张中,松弛中,低)和后背(4个级别:“ u / o /ɔ”,“ y /ø/œ”,“ i / e /ɛ”,“一种”)。高度的前三个级别与前三个级别的后退度结合在一起。但是,“身高”和“后背度”的最后一个级别不会与其他级别组合。这是数据集:

mydata <- structure(list(Vowel = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 7L, 7L, 7L, 7L, 7L, 7L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 8L, 8L, 8L, 8L, 8L, 8L, 3L, 3L, 3L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 6L, 6L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L), .Label = c("i", "e", "ɛ", "y", "ø", "œ", "u", "o", "ɔ", "a"), class = "factor"), distance = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("kp", "kt", "pt"), class = "factor"), Language = structure(c(1L, 1L, 1L,2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("French", "non-French"), class = "factor"), mean.dist = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2), sd.dist = c(0.120615504010632, 0.118412385311871, 0.121089776083054,0.149894186723077, 0.110166165335677, 0.112299637986508, 0.12939908491593, 0.115632300556256, 0.1232208247066, 0.125522547778451, 0.113827845634038, 0.121622532807705, 0.104520174716336, 0.131041477127477, 0.1224839610439, 0.10573903647089, 0.123680869606973, 0.113250190933477, 0.124947205620186, 0.116567786191159, 0.116711788711068, 0.138375016263539, 0.113070353979588, 0.109816142316869, 0.123866497777528, 0.128030948746137, 0.117993379792773, 0.116683458325427, 0.109730767345153, 0.119279399582146, 0.112930388666133, 0.131538829985269, 0.113242022434118, 0.104718500032215, 0.120046958765089, 0.122703726831522, 0.132890891589065, 0.151465602666509, 0.111960868349181, 0.166536043706081, 0.127513073089371, 0.11256771231828, 0.121444207664181, 0.120177407513513, 0.121259920821102, 0.129021201733638, 0.113933865258814, 0.122710394065478,0.107994086824079, 0.122041679482675, 0.127232388195383, 0.106014219374484,0.125177224086912, 0.12457590962369, 0.147765885739041, 0.124409226413158, 0.112718016807198, 0.156949752351136, 0.114751218006903, 0.114895198849989), Backness = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,3L,3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("i/e/E", "y/ø/œ", "u/o/O", "a"), class = "factor"), Height = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L),.Label = c("high", "tense mid", "lax mid", "low"), class = "factor")), .Names = c("Vowel", "distance", "Language", "mean.dist", "sd.dist", "Backness", "Height"), row.names = c(NA, -60L), class = "data.frame")

当我尝试使用下面的代码(使用fill = Height)绘制此数据集时,Height =“ low”和Backness =“ a”的组合的条形宽度非常大且难看(我猜是宽度,即其他“背面度”水平的三个细条的总和)。

ggplot(mydata, 
 aes(factor(Backness), mean.dist, fill=Height)) + geom_bar(stat="identity", position=position_dodge(0.9)) + geom_errorbar(aes(ymin=mean.dist-sd.dist, ymax=mean.dist+sd.dist), width=.2, position=position_dodge(0.9)) + facet_wrap(~Language+distance) + theme_bw() + ylab("d") + xlab("Vowel") + theme(axis.text=element_text(size=14),
                                axis.title=element_text(size=14), 
                                strip.text=element_text(size=14), 
                                legend.text = element_text(size=12)) + scale_fill_grey( start = 0.4, end = 0.8)

enter image description here

如何使该条像情节上的细条一样细?感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

即将发布的ggplotpreserve有一个新的position_dodge自变量,允许您想要的行为

ggplot(mydata, aes(factor(Backness), mean.dist, fill = Height)) + 
  geom_col(position = position_dodge(0.9, 'single')) + 
  geom_errorbar(aes(ymin=mean.dist-sd.dist, ymax=mean.dist+sd.dist), 
                width=.2, position=position_dodge(0.9, 'single')) + 
  facet_wrap(~Language+distance) + 
  theme_bw() + ylab("d") + xlab("Vowel") + 
  scale_fill_grey( start = 0.4, end = 0.8)

enter image description here