ggplot2:未在minor_breaks显示的滴答声

时间:2017-09-22 11:13:29

标签: r ggplot2

这可能是由于咖啡因缺乏造成的,但我不能minor_breaksggplot2中产生轻微的刻度线。例如,

# Load data
data(mtcars)

# Load library 
library(ggplot2)

# Create plot
p <- ggplot(data = mtcars, aes(x = wt, y = mpg)) 
p <- p + geom_point()
p <- p + scale_x_continuous(minor_breaks = seq(2.5, 4.5, by =1))
print(p)

这会产生以下情节:

enter image description here

注意:2.5,3.5和4.5处没有小刻度线。也没有错误或警告。现在,如果我只是将minor_breaks更改为breaks,那么主要刻度标记会出现在人们期望的位置,这让我觉得我的语法正确而且只是由于某种原因,小刻度标记是不可见的。指出我的错误,并获得丰厚的电子高五。

1 个答案:

答案 0 :(得分:0)

您提供的图片不是由您发布的代码生成的。您似乎正在使用一个主题:也许您删除了应用该主题的代码行,或者您在某处默认设置了一个主题而您忘记了它。无论哪种方式,某些主题都会覆盖 minor_breaks 的默认效果。插图:

您的代码:

data(mtcars)
library(ggplot2)
ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_point() +
scale_x_continuous(minor_breaks = seq(2.5, 4.5, by =1))

产生这个:

enter image description here

要查看 minor_breaks 和网格线之间的连接,请将值更改为类似:scale_x_continuous(minor_breaks = seq(2.3, 4.3, by =1)):

enter image description here

我相信您使用的是经典主题:如果您在前面的代码中添加 +theme_classic(),您会得到:

enter image description here

显示由 minor_breaks 设置的网格线被覆盖并消失。