记录比例x,数据为x = 0

时间:2017-11-07 18:44:02

标签: r ggplot2

As shown in this plot,当使用带有以下代码的对数刻度时,有没有办法扩展左边距以显示x = 0处的数据点,因此x = 0处的数据点将被完全绘制而不是被切割为在剧情左侧显示约一半?

x <- c(0, 0, 0, 0, 0, 0, 0.0095, 0.0095, 0.0095, 0.03, 0.03, 0.03, 0.095, 0.095, 0.095, 0.3, 0.3, 0.3)
y <- c(23975, 16852, 18924, 21086, 15940, 16308, 17443, 24729, 13513, 30260, 31883, 35960, 55485, 58366, 35607, 58119, 52414, 53990)
data <- as.data.frame(cbind(x, y))

p <-  ggplot(data.frame(x=c(min(data$x), max(data$x))), aes(x=x)) + 
      geom_point(data, mapping=aes(x=x, y=y))  +
      scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
                    labels = trans_format("log10", math_format(10^.x)))
p

2 个答案:

答案 0 :(得分:-1)

快速&#39; n&#39;脏的,不是最佳的解决方法:关闭裁剪并移除y轴。

library(ggplot2)
library(grid)

x <- c(0, 0, 0, 0, 0, 0, 0.0095, 0.0095, 0.0095, 0.03, 0.03, 0.03, 0.095, 0.095, 0.095, 0.3, 0.3, 0.3)
y <- c(23975, 16852, 18924, 21086, 15940, 16308, 17443, 24729, 13513, 30260, 31883, 35960, 55485, 58366, 35607, 58119, 52414, 53990)
data <- as.data.frame(cbind(x, y))

p <-  ggplot(data.frame(x=c(min(data$x), max(data$x))), aes(x=x)) + 
  geom_point(data, mapping=aes(x=x, y=y))  +
  scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^(.x)))) + 
  theme(axis.line.y = element_blank())

p <- ggplotGrob(p)
i <- which(p$layout$name == 'panel')
p$layout[i,'clip'] <- 'off'
grid.draw(p)

enter image description here

答案 1 :(得分:-1)

将以下内容添加到ggplot并运行

coord_cartesian(xlim = c(-0.2, 0.5), expand = TRUE)

请微调xlim以获得更好的效果。