某些期刊的惯例是仅在绘图中显示x和y轴,而不显示整个绘图区域周围的框。如何在ggplot2中实现呢?我尝试了从HEREtheme_minimal_cb_L,但似乎擦除了绘图周围的整个方框(不离开x和y轴),如下所示:

这是我正在使用的代码:

dat <- structure(list(x = c(0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1, 1.05,
    1.1, 1.15, 1.2, 1.25, 1.3), y1 = c(34, 30, 26, 23, 21, 19, 17,
    16, 15, 13, 12, 12, 11), y2 = c(45, 39, 34, 31, 28, 25, 23, 21,
    19, 17, 16, 15, 14)), .Names = c("x", "y1", "y2"), row.names = c(NA,
    -13L), class = "data.frame")


library(reshape2); library(ggplot2)
dat2 <- melt(dat, id='x')

theme_minimal_cb_L <- function (base_size = 12, base_family = "", ...){
  modifyList (theme_minimal (base_size = base_size, base_family = base_family),
              list (axis.line = element_line (colour = "black")))
}

ggplot(data=dat2, aes(x=x, y=value, color=variable)) +
    geom_point(size=3) + geom_line(size=.5) +
    theme_minimal_cb_L()

最佳答案

我相信您是在说您只想要绘图左侧和底部的线-对吗?

theme(panel.border = element_blank(), axis.line = element_line())

panel.border删除整个边框,然后使用axis.line添加x和y轴线。

编辑:由于您发布的其他主题内容对我不起作用。我将发布我实际运行的代码
dat <- structure(list(x = c(0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1, 1.05,
    1.1, 1.15, 1.2, 1.25, 1.3), y1 = c(34, 30, 26, 23, 21, 19, 17,
    16, 15, 13, 12, 12, 11), y2 = c(45, 39, 34, 31, 28, 25, 23, 21,
    19, 17, 16, 15, 14)), .Names = c("x", "y1", "y2"), row.names = c(NA,
    -13L), class = "data.frame")


library(reshape2); library(ggplot2)
dat2 <- melt(dat, id='x')

ggplot(data=dat2, aes(x=x, y=value, color=variable)) +
    geom_point(size=3) + geom_line(size=.5) +
    theme(panel.border = element_blank(), axis.line = element_line())

# or using the template you used from talkstats...
ggplot(data=dat2, aes(x=x, y=value, color=variable)) +
    geom_point(size=3) + geom_line(size=1.5) +
    scale_y_continuous(breaks=seq(0, 45, by=5)) +
    scale_x_continuous(breaks=seq(.7, 1.3, by=.05)) +
    theme_bw() + ylab("Sample Size") + xlab("Mean Difference") +
    theme(legend.position="bottom", legend.title=element_blank(),
        legend.key = element_rect(colour = 'white'),
        legend.background = element_rect(colour = "black")) +
    ggtitle("Sample Size vs. Mean Difference by Power") +
    theme(panel.border = element_blank(), axis.line = element_line())

关于r - 在ggplot2中仅绘制x和y轴边框(无框)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12883386/

10-12 17:50