ggplot顶部的x轴,带有不同的标签

时间:2016-02-02 17:29:25

标签: r ggplot2

我想在绘图顶部添加一些与x轴一致的标签。如何将cm3中的位移添加到此图的顶部,以便我在底部有立方英寸,在顶部有立方厘米?我应该用注释吗?刻度线怎么样?在这种情况下,我意识到这会产生一个丑陋的图形。 [为清晰起见而编辑。]

# Calculate displacement in cm3 -- add this to top of plot 
# above 100, 200, 300, 400 ticks and labels for cubic inches on bottom
cm3.lab <- seq(100,400,100) * 16.3871
p <- ggplot(mtcars, aes(disp, mpg))
p + geom_point(size=5) + xlab("Cubic inches")
p

1 个答案:

答案 0 :(得分:2)

您可以更好地控制标签&amp;带有scale_x_continuous的刻度线:

p + geom_point(size=5) + scale_x_continuous(name="Displacement in cubic centimeters", breaks = c(100, 200, 300, 400), labels=c("100" = "100", "200" = "200", "300" = "300", "400" = "400"));

有关更多选项,请参阅?scale_x_continuous

注意:立方厘米和立方英寸不是一回事。一定要选对了!

相关问题