R ggplot2 中存在图例符号(但不包括文本)

问题描述 投票:0回答:1

我不久前做了一个复杂的图形,看起来像这样:

The correct version

不幸的是,从那时起,我的计算机死机了,我丢失了用于生成该图的代码的最新功能版本。我拥有其中的大部分内容,但是尽管存在图例符号,但图例文本不可见(见下文,我已删除图标以使其更易于阅读)。 有人可以帮助我找出为什么图例文本不可见而不破坏其余代码吗?我还缺少 x 轴标签? 如果有更简单的方法来实现相同的输出,我洗耳恭听,我知道我不是最高效的编码器。

The current, broken version

library(tidybayes) 
library(ggtext) 
library(grid) 
library(ggplot2) 
library(prismatic)
library(ggnewscale) 
library(dutchmasters)

sal_abundance.1 <- structure(list(MEANS = c(53.02737, 136.56263, 64.93336, 107.23139, 
                               51.8929, 59.82375, 72.38172, 80.82774, 96.72059, 77.10681, 120.15648, 
                               122.13566, 88.86555, 106.58235, 113.11388), CI2.5 = c(44, 129, 
                                                                                     56, 96, 44, 51, 62, 69, 85, 66, 107, 109, 78, 93, 99), CI97.5 = c(63, 
                                                                                                                                                       140, 75, 119, 60, 69, 84, 93, 109, 89, 134, 136, 101, 121, 128
                                                                                     ), factor.salamanders.tree. = structure(c(1L, 2L, 1L, 2L, 1L, 
                                                                                                                               2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L), levels = c("B", "H"), class = "factor"), 
                     factor.salamanders.site. = structure(c(1L, 1L, 1L, 1L, 1L, 
                                                            1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("C", 
                                                                                                                "M"), class = "factor"), site_plot = structure(1:15, levels = c("Chesterfield_9B", 
                                                                                                                                                                                "Chesterfield_2H", "Chesterfield_4B", "Chesterfield_3H", 
                                                                                                                                                                                "Chesterfield_6B", "Chesterfield_7H", "MacLeish_2B", "MacLeish_1H", 
                                                                                                                                                                                "MacLeish_3H", "MacLeish_4B(M)", "MacLeish_5H", "MacLeish_6H", 
                                                                                                                                                                                "MacLeish_7B", "MacLeish_8H", "MacLeish_9H"), class = "factor"), 
                     site = c("Chesterfield", "Chesterfield", "Chesterfield", 
                              "Chesterfield", "Chesterfield", "Chesterfield", "MacLeish", 
                              "MacLeish", "MacLeish", "MacLeish", "MacLeish", "MacLeish", 
                              "MacLeish", "MacLeish", "MacLeish"), plot_pair = c("Pair 1", 
                                                                                 "Pair 1", "Pair 2", "Pair 2", "Pair 3", "Pair 3", "Pair 1", 
                                                                                 "Pair 1", "Pair 1", "Pair 2", "Pair 2", "Pair 2", "Pair 3", 
                                                                                 "Pair 3", "Pair 3")), row.names = c(NA, -15L), class = "data.frame")

# Some specific colors
Little <- unique(dutchmasters$little_street)

# Create plot labels for each mean point
labels <- c("Plot 9","Plot 2","Plot 4","Plot 3","Plot 6","Plot 7","Plot 2","Plot 1","Plot 3","Plot 4","Plot 5","Plot 6","Plot 7","Plot 8","Plot 9")

# Establish plot theme, width, and font
theme_set(theme_light(base_size = 18, base_family = "Roboto"))



# Generate the plot
t <- ggplot(sal_abundance.1, aes(y = factor(site_plot), 
                                 x = MEANS, xmin = CI2.5, 
                                 xmax = CI97.5, color = factor.salamanders.tree., linetype = factor.salamanders.tree.)) +
# Weirdly the following point intervals will not run unless this one runs first  
geom_pointinterval(position = position_dodge(width = .3), 
                     color = "transparent", 
                     interval_size = 3, 
                     point_size = 3, 
                     linetype = "blank", 
                     show.legend = FALSE) +
  scale_color_manual(
    name = "Means",
    values = c("Site" = "grey30", "*B. lenta*" = clr_saturate(Little[8],shift = .5), "*T. canadensis*" = Little[4])) +
  scale_linetype_manual(
    name = "Means",
    values = c("Site" = 1, "*B. lenta*" = 2, "*T. canadensis*" = 2),
    guide = guide_legend(override.aes = list(alpha = .8,shape = NA))) +
  geom_segment(aes(x = 97.54342, y = 15.6, xend = 97.54342, yend = 6.6, 
                   color = "Site", linetype = "Site"), alpha = .015, inherit.aes = FALSE, size = 1) +
  geom_segment(aes(x = 106.5894, y = 15.6, xend = 106.5894, yend = 6.6, 
                   color = "*T. canadensis*", linetype = "*T. canadensis*"), alpha = .015, inherit.aes = FALSE, size = 1) +
  geom_segment(aes(x = 79.45136, y = 15.6, xend = 79.45136, yend = 6.6, 
                   color = "*B. lenta*", linetype = "*B. lenta*"), alpha = .015, inherit.aes = FALSE, size = 1) +
  geom_segment(aes(x = 78.9119, y = 0.4, xend = 78.9119, yend = 6.6, 
                   color = "Site", linetype = "Site"), alpha = .015, size = 1, show.legend = FALSE) +
  geom_segment(aes(x = 101.2059, y = 0.4, xend = 101.2059, yend = 6.6, 
                   color = "*T. canadensis*", linetype = "*T. canadensis*"), alpha = .015, size = 1, show.legend = FALSE) +
  geom_segment(aes(x = 56.61788, y = 0.4, xend = 56.61788, yend = 6.6, 
                   color = "*B. lenta*", linetype = "*B. lenta*"), alpha = .015, size = 1, show.legend = FALSE) +
  annotate("text", x = c(39.5,40.75), y = c(15.2,6.2), family = "Roboto", size = 6, lineheight = .9,
           label = paste0(c("MacLeish","Chesterfield")), color = "grey30") +
  annotate("text", x = 37, y = c(.7,2.9,4.9,6.9,9.9,12.9), family = "Roboto", size = 4, lineheight = .9,
           label = paste0(rep(c("Pair 1","Pair 2","Pair 3"),2)), color = "grey30") +
  new_scale_color() +
  geom_hline(yintercept = 6.6, color = "gray") +
  geom_hline(yintercept = c(2.6,4.6,9.6,12.6), color = "gray30", linetype = "dotted") +
  scale_color_manual(
    name = "Tree Species",
    labels = c("*B. lenta*","*T. canadensis*"),
    values = c(clr_saturate(Little[8],shift = .5), Little[4]),
    aesthetics = c("color","fill")) +
  geom_pointinterval(aes(color = factor.salamanders.tree.), 
                     position = position_dodge(width = .3), 
                     linetype = "solid", interval_size = 3, point_size = 3, show.legend = FALSE) +
  geom_point(position = position_dodge(width = .3), 
             size = 3, stroke = 3, color = "white", show.legend = FALSE) +
  geom_point(aes(color = factor.salamanders.tree.),
             position = position_dodge(width = .3), size = 3, stroke = 3, alpha = .3, show.legend = TRUE) +
  scale_x_continuous(expand = c(0,0),limits = c(35,150)) +
  ggtext::geom_richtext(
    aes(label = labels, vjust =-.3,
        color = factor.salamanders.tree.),
        size = 3.5, fill = NA, label.color = NA, show.legend = FALSE
  ) +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.title = element_text(size = 16, color = "grey30"),
    axis.text.x = element_text(family = "Roboto", size = 12),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.title.y = element_blank(),
    axis.title.x = element_markdown(),
    legend.text = element_markdown(size = 12, color = "grey30"),
    legend.title = element_text(size = 16, color = "grey30"),
    legend.background = element_rect(fill = "transparent")) +
  coord_cartesian(clip = "off") +
  labs(x = "Mean *P. cinereus* abundance") 
t
r ggplot2 visualization
1个回答
0
投票

有点奇怪,我尝试用

tiff()
dev.off()
保存你的情节,它似乎有效。 我没有修改关于你的图的任何内容(
t
),所以如果你运行你的代码,然后运行下面的我的代码,你应该在你的工作目录中找到该图(名为 test1.tiff)。 也许问题与您正在创建的图像的尺寸有关? 1100x600 对我来说似乎工作得很好(除非我错过了一些东西)

tiff(paste0(getwd(), "/test1.tiff"), height = 600, width = 1100)

t

dev.off()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.