更改边框的线型

时间:2018-10-26 17:00:07

标签: r

我想在图形中将面板边框的线型从实线更改为虚线。我尝试在主题中使用panel.border和panel_border,但是R一直告诉我它无法在主题中找到这两个函数。

2 个答案:

答案 0 :(得分:6)

我认为使用theme选项很难做到。即使面板间距为0,我也可以肯定它使用element_rect绘制面板边框。进行theme修改后,要获取所需的图,则需要在知道该构面是左,右还是中心构面的情况下,修改每个面板边框的1或2条线。

要解决此问题,我们可以改用hlinevline。哈克,但它可以工作:

hline_df = expand.grid(visit = unique(df$visit), yint = c(-Inf, Inf))
vline_df = expand.grid(visit = unique(df$visit), xint = c(-Inf, Inf)) %>%
  mutate(no_dash = !(
    (xint == Inf & visit == tail(levels(visit), 1))  |
    (xint == -Inf & visit == head(levels(visit), 1))
  ))


ggplot(df, aes(x=avisit, y=mean, group=Type, color=Type, shape=Type)) + 
       scale_y_continuous(breaks=seq(0,18,2), limits=c(0, 18)) +
       geom_point(position=pd, cex=2) +
       xlab("") +
       ylab("Mean")  +
       scale_colour_manual(values=c("blue", "red")) +
       scale_shape_manual(values=c("triangle", "circle")) + 
       coord_cartesian(ylim = c(0, 18)) +
       facet_grid(.~factor(visit), scales = "free_x", space ="free_x",switch = "both") +
       geom_hline(data = hline_df, aes(yintercept = yint)) +
       geom_vline(data = vline_df, aes(xintercept = xint, linetype = no_dash), show.legend = FALSE) +
       theme_bw()  +
       theme(axis.text.y = element_text(margin = margin(r = 0)),  
             panel.spacing = unit(0, "mm"),                        
             strip.background = element_blank(),
             legend.title=element_blank(),
             strip.placement = "outside",
             legend.background = element_rect(color="black", fill="white", size=0.5, linetype="solid"), 
             legend.direction = "horizontal",
             panel.grid.minor = element_line(colour="white", linetype="dashed"),
             panel.grid.major = element_line(colour = "white",linetype="dashed"),
             panel.border = element_blank())

enter image description here

答案 1 :(得分:0)

element_rect采用线型参数。它可能会在边界的顶部绘制轴线,反之亦然,因此您也可以修改axis.line。该主题将为您提供所需的内容,并且会更加优雅: ggplot() + theme(axis.line = element_line(linetype = 3), panel.border = element_rect(linetype =3)