**编辑,这里有两个很棒的解决方案,一个被标记为答案,但@hrbrmstr提供了一个很好的解决方案,它结合了两个ggplots,适用于这个简单的情节。*
这是代码
breaks.major <- c(0,15,37.5,52.5,67.5,82.5,95,100) #defines the midpoints of the categories (label locations)
breaks.minor <- c(30,45,60,75,90) #defines the edges of the categories (second label set I need)
labels.minor <- c("","Extremely \nDissatisfied","Dissatisfied","Uncertain","Satisfied","Very \nSatisfied","Extremely \nSatisfied","")
lims =c(0,100)
g <- ggplot(mpg, aes(class))+
geom_bar()+
coord_flip()+
scale_y_continuous(limit = lims, minor_breaks = breaks.minor, breaks = breaks.major, labels = labels.minor) +
theme(panel.grid.major.x = element_blank()) +
theme(panel.grid.major.y = element_blank()) +
theme(axis.ticks.x=element_blank()) +
theme(axis.title= element_blank())
它产生了这个情节:
我需要有两组X轴标签,一组显示类别名称(即通过labels.minor
已经存在的“满意”等),另一组显示{{1位置(对应于类别限制,即垂直面板网格线)。我需要当前的breaks.minor
标签低于所需的附加标签。
我目前使用换行符进行此操作,以便数字和类别都在一个长字符串中,但是间距会变得有趣,而且调整大小。我可以使用文本框(我假设)这样做,ggplot中有没有办法?
如果您将当前标签放在其中心部分(例如“非常满意”偏离中心),则需要额外积分
这是我想要的输出(请原谅我的'mspaint')
答案 0 :(得分:6)
我认为这可以满足您的需求:
library(ggplot2)
library(grid)
library(gtable)
library(gridExtra)
breaks.major <- c(0, 15, 37.5, 52.5, 67.5, 82.5, 95, 100)
breaks.minor <- c(30, 45, 60, 75, 90)
labels.minor <- c("", "Extremely\nDissatisfied", "Dissatisfied", "Uncertain",
"Satisfied", "Very\nSatisfied", "Extremely\nSatisfied", "")
lims <- c(0, 100)
# build the main plot with the text axis
gg1 <- ggplot(mpg, aes(class))
gg1 <- gg1 + geom_bar()
gg1 <- gg1 + scale_y_continuous(expand=c(0,0), limit=lims,
minor_breaks=breaks.minor,
breaks=breaks.major,
labels=labels.minor)
gg1 <- gg1 + coord_flip()
gg1 <- gg1 + theme(panel.grid.major.x=element_blank())
gg1 <- gg1 + theme(panel.grid.major.y=element_blank())
gg1 <- gg1 + theme(axis.ticks.x=element_blank())
gg1 <- gg1 + theme(axis.title=element_blank())
# let ggplot2 do the work of building the second axis
gg2 <- ggplot(mpg, aes(class))
gg2 <- gg2 + scale_y_continuous(expand=c(0,0), limit=lims,
breaks=c(0, breaks.minor, 100))
gg2 <- gg2 + coord_flip()
gg2 <- gg2 + theme(axis.ticks.x=element_blank())
gg2 <- gg2 + theme(axis.text.x=element_text(hjust=c(0, 0.5, 0.5, 0.5, 0.5, 0.5, 1)))
gt1 <- ggplot_gtable(ggplot_build(gg1))
gt2 <- ggplot_gtable(ggplot_build(gg2))
axis2 <- grid.arrange(gt2$grobs[[5]])
gt <- gtable_add_rows(gt1, unit(0.1, "null"), 4)
grid.arrange(gtable_add_grob(gt, axis2, t=5, l=4, b=5, r=4))
答案 1 :(得分:5)
也许是这样的。请注意两个轴的expand
设置,以处理正确的间距和类别名称的位置。
图中的标签不是真正偏离中心,它们位于类别边界的中心。只是默认情况下,轴会进一步扩展。
如果你想获得更多的幻想,你也可以在绘图区域之外绘制,但它需要更多的fiddeling。 This question应该让你开始。
ggplot(mpg, aes(class))+
geom_bar()+
geom_text(data = data.frame(br = breaks.minor), aes(y = br, label = br, x = 7.75),
size = 4, col = 'grey30') +
coord_flip()+
scale_y_continuous(limit = lims, minor_breaks = breaks.minor,
breaks = breaks.major, labels = labels.minor,
expand = c(0, 0)) +
scale_x_discrete(expand = c(0.05, 0)) +
theme(panel.grid.major.x = element_blank()) +
theme(panel.grid.major.y = element_blank()) +
theme(axis.ticks.x=element_blank()) +
theme(axis.title= element_blank())
答案 2 :(得分:2)