【问题标题】:Extend ggplot x-axis lines past range of data将 ggplot x 轴线延伸到数据范围之外
【发布时间】:2015-10-29 16:01:08
【问题描述】:

我正在尝试构建一堆 ggplot2 密度图,如下所示:

我已经排列/限制了 x 轴,以便这些 grpah 使用 gridExtra 包堆叠,刻度线完美排列。但是,在这样做之前,我认为是实心 x 轴标记,结果却是密度图底部的底部“标记”线:

有没有在某种 x 轴上加回来?没有它,这些情节看起来有些赤裸/空虚。我理解它更清楚地表明了数据的限制,但它看起来没有完成和损坏。

编辑

这是我正在使用的代码:

g <- ggplot(df_L, aes(x=values, linetype= type)) + 
geom_density() + 
ggtitle(expression('Low Region: '~LI[i]~'and'~WI[i])) +
scale_x_continuous(breaks = c(seq(0,100,10)), expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
coord_cartesian(xlim = c(0,100)) + 
theme(text = element_text(size=20),
      plot.title = element_text(size=14, vjust=1.5, hjust=0.5),
      axis.title.x=element_blank(),
      axis.title.y = element_blank(),
      legend.position = c(0.1, 0.75),
      legend.text.align = 0,
      legend.box = 'horizontal',
      legend.margin = unit(45.0, 'line'),
      legend.text=element_text(size=14,vjust=0,hjust=0),
      legend.key.height = unit(1, 'line'),
      legend.key.width = unit(1, 'line'),
      panel.background = element_rect(fill = "white")) + 
scale_linetype_manual(values=c(1,2,3),
                      labels=c(expression(LI[i]),expression(WI[i]))) +
guides(linetype = guide_legend(title=NULL)) 

g

【问题讨论】:

  • 您是否以任何方式移除了 x 轴? (使用主题?)如果不是这样,显示一些代码/示例数据(即 reproducible example )会有所帮助。
  • 添加geom_hline(yintercept = 0) 就足够了吗?

标签: r plot ggplot2 probability-density


【解决方案1】:

我认为问题在于您使用的主题(默认)没有指定的 x 轴(或 y 轴),但轴位置是由网格隐含的。因此,您需要专门添加轴,例如使用 +theme_bw() 或在主题中添加一些内容。我已经做到了(红色,你真的可以看到它):

set.seed(124)
df_L <- data.frame(values=rnorm(1000,500,200),type=sample(LETTERS[1:3],1000,T))

g <- ggplot(df_L, aes(x=values, linetype= type)) + 
  geom_density() + 
  ggtitle(expression('Low Region: '~LI[i]~'and'~WI[i])) +
  scale_x_continuous(breaks = c(seq(0,100,10)), expand = c(0,0)) +
  scale_y_continuous(expand = c(0,0)) +
  coord_cartesian(xlim = c(0,100)) + 
  theme(text = element_text(size=20),
        plot.title = element_text(size=14, vjust=1.5, hjust=0.5),
        axis.title.x=element_blank(),
        axis.title.y = element_blank(),
        legend.position = c(0.1, 0.75),
        legend.text.align = 0,
        legend.box = 'horizontal',
        legend.margin = unit(45.0, 'line'),
        legend.text=element_text(size=14,vjust=0,hjust=0),
        legend.key.height = unit(1, 'line'),
        legend.key.width = unit(1, 'line'),
        panel.background = element_rect(fill = "white"),
        axis.line=element_line(colour="red",size=2)) + 
  scale_linetype_manual(values=c(1,2,3),
                        labels=c(expression(LI[i]),expression(WI[i]))) +
  guides(linetype = guide_legend(title=NULL)) 

【讨论】:

    猜你喜欢
    • 2012-12-03
    • 2018-05-30
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    相关资源
    最近更新 更多