Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R theme_bw()将从我的绘图中去除所有颜色_R_Ggplot2 - Fatal编程技术网

R theme_bw()将从我的绘图中去除所有颜色

R theme_bw()将从我的绘图中去除所有颜色,r,ggplot2,R,Ggplot2,上面的代码是漂亮的黑白主题,带有一条红色的横线。下面的代码也应该是黑白主题,这次是一条红色的垂直线。但情节完全没有色彩。为什么theme\u bw()要从我下面的图中去掉所有颜色 library(tidyverse) ggplot(mtcars, aes(cyl, mpg)) + geom_point() + theme_bw() + geom_hline(aes(yintercept = 20), color = "red") 库(tidyverse) 图书馆(lubrid

上面的代码是漂亮的黑白主题,带有一条红色的横线。下面的代码也应该是黑白主题,这次是一条红色的垂直线。但情节完全没有色彩。为什么
theme\u bw()
要从我下面的图中去掉所有颜色

library(tidyverse)
ggplot(mtcars, aes(cyl, mpg)) + 
  geom_point() + 
  theme_bw() + 
  geom_hline(aes(yintercept = 20), color = "red")
库(tidyverse)
图书馆(lubridate)
df%
变异(年=年系数(年(日)))
ggplot(df、aes(日期、值))+
几何线()
几何线(
xintercept=as.numeric(df$date[yday(df$date)==1]),color=“红色”
) + 
比例尺x日期(
date_labels=“%b”,breaks=scales::pretty_breaks(),expand=c(0,0)
) +
刻面网格(.~year,space='free_x',scales='free_x',switch='x')+
实验室(x=”“)+
主题(基本尺寸=14,基本系列='mono')+
主题(panel.grid.minor.x=element_blank())+
主题(panel.spating.x=单位(0,“线”))+
主题(strip.placement='outside',strip.background.x=element\u blank())

您可以使用
panel.border=element\u blank()
inside
theme

library(tidyverse)
library(lubridate)

df <- 
  tibble(
    date = as.Date(41000:42000, origin = "1899-12-30"), 
    value = c(rnorm(500, 5), rnorm(501, 10))
  ) %>% 
  mutate(year = as.factor(year(date)))

ggplot(df, aes(date, value)) + 
  geom_line() + 
  geom_vline(
    xintercept = as.numeric(df$date[yday(df$date) == 1]), color = "red"
  ) + 
  scale_x_date(
    date_labels = "%b", breaks = scales::pretty_breaks(), expand = c(0, 0)
  ) +
  facet_grid(.~ year, space = 'free_x', scales = 'free_x', switch = 'x') +
  labs(x = "") +
  theme_bw(base_size = 14, base_family = 'mono') +
  theme(panel.grid.minor.x = element_blank()) + 
  theme(panel.spacing.x = unit(0, "line")) +
  theme(strip.placement = 'outside', strip.background.x = element_blank()) 


更新 要使用刻面并使红线不与刻面边界重叠,这是一个有点“粗糙”的解决方案:

首先,绘制无面板边框的图形

ggplot(df, aes(date, value)) +
  geom_line() +
  geom_vline(
    xintercept = as.numeric(df$date[yday(df$date) == 1]), color = "red") +
  scale_x_date(
    date_labels = "%b", breaks = scales::pretty_breaks(), expand = c(0, 0)) +
  facet_grid(.~ year, space = 'free_x', scales = 'free_x', switch = 'x') +
  labs(x = "") +
  theme_bw(base_size = 14, base_family = 'mono') +
  theme(
      panel.grid.minor.x = element_blank(),
      panel.spacing.x = unit(0, "line"),
      panel.border = element_blank(),
      strip.placement = 'outside',
      strip.background.x = element_blank())


这有点麻烦,因为您需要将绘图分为两个步骤,以便能够提取面板的y轴范围

另一个黑客,如果你愿意深入研究的话:

ylim <- ggplot_build(gg)$layout$panel_params[[1]]$y.range
gg <- gg +
    geom_rect(
        xmin = min(df$date), xmax = max(df$date),
        ymin = ylim[1], ymax = ylim[2],
        fill = NA, colour = "black")

gp您只需增加线条的大小(
geom_vline(size=2,…)
),所有这些都是因为您将绘图拆分为面,这些边覆盖了线条(或删除面)。@PoGibas感谢您的帮助!但是如何消除刻面黑线呢?现在的结果是我得到了
size=2
红线,上面画了一条
size=1
黑线。看起来像赛车条纹(在本例中我不想要)删除刻面?这里是一个,答案使用
theme\u classic()
,并在顶部和右侧添加线条。谢谢,我也考虑过,但我真的需要边框。“它很有用,所以我会投它的票。”@stackinator没问题。我认为你不得不在这里做出妥协。删除面板边框,或删除镶嵌面(如PoGibas建议的),或增加线条的大小以在黑色边框顶部绘制。我可能缺少一些内容,但无法在黑色边框顶部绘制。我只能在他们下面画画。看起来,无论您将
geom\u vline()
放置在代码中的何处,面边界总是最后绘制,因此位于ggplot绘制的所有其他线的顶部。@stackinator是的,红色
geom\u vline
总是首先绘制。我添加了一个有点“黑客”的解决方案,它应该满足您的需求。请看一看,这就解决了。非常感谢。我通常会在标记为已解决问题之前24小时给出答案。看看最优雅的解决方案是什么。我明天会这么做,我想很可能就是这样。
ylim <- ggplot_build(gg)$layout$panel_params[[1]]$y.range
gg <- gg +
    geom_rect(
        xmin = min(df$date), xmax = max(df$date),
        ymin = ylim[1], ymax = ylim[2],
        fill = NA, colour = "black")
gp <- ggplotGrob(p) # where p is the original ggplot object

# for each panel grob, change the order of grobs such that the grob corresponding
# to geom_vline (should have a name like "GRID.segments.XXXX") lies above the grob
# corresponding to the facet outline (should have a name like "panel.border..rect.XXXX")
for(i in grep("panel", gp$layout$name)){
  old.order <- gp$grobs[[i]]$childrenOrder
  new.order <- c(old.order[-grep("segments", old.order)],
                 old.order[grep("segments", old.order)])
  gp$grobs[[i]]$childrenOrder <- new.order
}

grid::grid.draw(gp)