【问题标题】:R | ggplot2 | (remove tick marks + remove panel border) but keep axis lines右 | ggplot2 | (删除刻度线+删除面板边框)但保留轴线
【发布时间】:2017-12-17 01:21:48
【问题描述】:

这里是新手,请善待和温柔! :)

我正在处理以下数据集和 R 脚本:

#Create pvalue ranges
pvalue <- c(".000 - .005",".005 - .010",".010 - .015",".015 - .020",".020 - .025",".025 - .030",".030 - .035",".035 - .040",".040 - .045",".045 - .050")

#Create frequency counts
count <- c(5000,4000,3100,2540,2390,2260,2150,2075,2050,2025)

dat <- data.frame(pvalue = pvalue, count = count)

#Create plot
myPlot <- ggplot(data=dat, aes(x=pvalue, y=count, group=1)) +
    geom_line() +
    geom_point() +
    geom_vline(xintercept=which(dat$pvalue == '.045 - .050'), linetype = "dashed") +
    theme_bw() +
    theme(axis.text.x = element_text(angle=90),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.background = element_blank()) +
    theme(panel.border = element_blank()) +
    ggtitle(paste("Insert Plot Title Here")) +
    labs(x = "insert x-axis title here", y = "insert y-axis title here") +
    theme(plot.title = element_text(lineheight=0.5,family = "TNR")) +
    theme(axis.line.x = element_line(color="black"),
          axis.line.y = element_line(color="black")) +
    scale_y_discrete(breaks=NULL)

myPlot

上述数据集和 R 脚本生成以下图:

Note that I do not have enough "points" to embed an image so a link to the image has been created by Stack Overflow

图像检查显示左侧面板边框(或垂直轴)缺失。我希望将左面板边框包含在图中。但是,我想排除左面板边框(或垂直轴)上的刻度线。综上所述,我想要的情节将

  1. 为 y 轴添加一条垂直线,
  2. 为 x 轴添加一条水平线,
  3. 移除沿 y 轴(垂直轴)的刻度线
  4. 移除顶部面板边框
  5. 移除右侧面板边框

上面的 R 脚本处理了这个列表中的 #2-5。但是,我已经尝试过并且无法弄清楚如何处理此列表中的 #1 - 尽管在我的 R 脚本中包含以下内容:

theme(axis.line.x = element_line(color="black"),
          axis.line.y = element_line(color="black")) +

有人可以帮我制作所需的图像吗?非常感谢:)

【问题讨论】:

  • dat 不是在这里创建的,所以如果没有至少 一些 数据,我们无法真正帮助您...
  • 我猜dat &lt;- data.frame(pvalue = pvalue, count = count)
  • @JeanneChaudanson 我认为其他人更新了我的代码以包含 dat
  • 感谢@Axeman 添加到我的代码中:)

标签: r ggplot2 axis-labels border-layout yaxis


【解决方案1】:

scale_y_discrete(breaks = NULL) 打破了 y 轴,因为它解释为什么都不显示。

删除那行我们有y axis,然后我们可以删除ticks和text:

library(ggplot2)

ggplot(data=dat, aes(x=pvalue, y=count, group=1)) +
  geom_line() +
  geom_point() +
  geom_vline(xintercept=which(dat$pvalue == '.045 - .050'), linetype = "dashed") +
  ggtitle(paste("Insert Plot Title Here")) +
  labs(x = "insert x-axis title here", y = "insert y-axis title here") +
  theme_bw() +
  theme(plot.title = element_text(lineheight=0.5,family = "TNR"),
        axis.line = element_line(),
        axis.ticks.y = element_blank(),        ## <- this line
        axis.text.y = element_blank(),         ## <- and this line
        axis.text.x = element_text(angle=90),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        panel.border = element_blank()) 

【讨论】:

  • 当我运行您提供的代码时,x 轴和 y 轴的线不会出现。否则,情节看起来不错。此外,我收到以下警告消息。有什么建议? > 警告消息:1:在 grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : 在 Windows 字体数据库中找不到字体系列 2: 在 grid.Call(L_textBounds, as. graphicsAnnot(x$label), x$x, x$y, : 在 Windows 字体数据库中找不到字体系列 3: 在 grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : 在 Windows 字体数据库中找不到字体系列
  • 将“axis.line = element_line()”更改为“axis.line.x = element_line(color = "black"), axis.line.y = element_line(color = "black")
  • 该警告很可能是由于element_text(lineheight=0.5,family = "TNR"),它来自您自己的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-19
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
  • 1970-01-01
  • 2023-02-05
  • 1970-01-01
相关资源
最近更新 更多