R 如何将ggplot2条形图的负x轴打断(标签)更改为正x轴打断(标签)?
我有一个数据框,看起来像这样:R 如何将ggplot2条形图的负x轴打断(标签)更改为正x轴打断(标签)?,r,ggplot2,R,Ggplot2,我有一个数据框,看起来像这样: df <- structure(list(gender = c("male", "male", "male", "female", "female", "female", "male", "male", "male", "male", "male", "female", "female", "female"), agegroup = c("-24", "25-34", "45-54", "-24", "25-34", "35-44", "-24", "
df <- structure(list(gender = c("male", "male", "male", "female", "female",
"female", "male", "male", "male", "male", "male", "female", "female",
"female"), agegroup = c("-24", "25-34", "45-54", "-24", "25-34",
"35-44", "-24", "25-34", "35-44", "65-", "unknown", "-24", "25-34",
"35-44"), N = c(-2, -4, -1, 3, 4, 1, -3, -14, -1, -1, -2, 3,
3, 1), N2 = c(2, 4, 1, 3, 4, 1, 3, 14, 1, 1, 2, 3, 3, 1), location = c("here",
"here", "here", "here", "here", "here", "there", "there", "there",
"there", "there", "there", "there", "there")), .Names = c("gender",
"agegroup", "N", "N2", "location"), row.names = c(NA, 14L), class = "data.frame")
看来我是有意的。剩下的就是把-10和-5变回正数。有没有一种方法可以在不改变绘图外观的情况下执行此操作?看起来您只需要将所有轴标签设置为正值,即取绝对值。我们可以使用
scale.*\u continuous
和标签
参数来实现这一点。请注意,在这种情况下,我们需要变换y轴,因为我们必须在coord_flip
之前参考该轴。pretty
功能可用于方便地生成一些漂亮的轴打断:
将此添加到绘图中:
scale_y_continuous(breaks = pretty(df$N), labels = abs(pretty(df$N))
@阿克斯曼,谢谢!如果事先不知道会有哪些标签,有没有办法通过编程更改标签?@Axeman,如果你将此作为答案添加,我会接受。再次感谢!
scale_y_continuous(breaks = pretty(df$N), labels = abs(pretty(df$N))