手动颜色和填充混合线/点

时间:2017-10-20 14:41:13

标签: r ggplot2

我试图在具有两个变量的图上模拟这个单变量图(图像1)上的线/点样式。我能得到的最接近的是第二张图片

Single variable

Multi

以下是我用于多线图的代码。

library(tidyverse)
library(ggthemes)

national_employment <- read.csv("Data/Tab/National/emplyment.csv", stringsAsFactors = FALSE) %>% 
  mutate(total_pop = total_pop * 1000,
         labor_force = labor_force * 1000,
         employed = employed * 1000,
         unemployed = unemployed * 1000,
         unemployed_rate = round(unemployed / labor_force, 4))

# Employement and Labor force visualizations
group_colors <- c('employed' = "#1076bc",
                  'labor_force' = '#939598')
group_labels <- c('employed' = 'Total Employed',
                  'labor_force' = 'Total Labor Force')
national_employment %>% 
  gather(employed, labor_force, key = type, value = total) %>% 
  ggplot(aes(year, total, color = type)) +
  geom_line(size = 1) +
  geom_point(shape=21, size = 2.5, stroke = 1.5) +
  labs(x = "Year", y = "Total",
       title = "U.S. National Employement",
       subtitle = "2017 statistics reflect August 2017 data",
       caption = "Source: U.S. Bureau of Labor Statistics") +
  scale_x_continuous(breaks = seq(2000, 2020, by = 5), limits = c(2000, 2020)) +
  scale_y_continuous(expand = c(0, 0), labels = comma, breaks = seq(100000000, 164000000, by = 10000000), 
                     limits = c(100000000, 161000000)) +
  scale_color_manual('Legend', values = group_colors, labels = group_labels) +
  theme_hc() +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text = element_text(size = 9.5, color = '#929598'),
        axis.ticks = element_blank(),
        axis.line.y = element_line(color = '#bcbec0',size = 1.5,lineend = 'butt'),
        axis.line.x = element_line(color = '#bcbec0',size = 1.5,lineend = 'butt'),
        plot.caption = element_text(hjust = -.12, color = '#bcbec0'),
        panel.grid.major = element_line(colour = "#d1d3d4", size = 0.5))

如何正确设置缩放颜色/填充以模拟第一个图形?

1 个答案:

答案 0 :(得分:1)

简单而虚拟的解决方案,但有效 - 使用粗边框(与背景颜色相同)制作点:

df <- data.frame(x = seq(1, 10), y = sample(1:3, 10, replace = TRUE))
library(ggplot2)
ggplot(df, aes(x, y)) +
    geom_line(size = 2, color = "#34B2EF") +
    geom_point(size = 3, fill = "#34B2EF", color = "white", shape = 21, stroke = 2) +
    theme_classic()

enter image description here