首页 > 解决方案 > 无法使用 ggplot2 显示线和点

问题描述

我有一个数据框df如下:

structure(list(date = structure(c(1L, 13L, 16L, 19L, 22L, 25L, 
28L, 31L, 34L, 4L, 7L, 10L, 2L, 14L, 17L, 20L, 23L, 26L, 29L, 
32L, 35L, 5L, 8L, 11L, 3L, 15L, 18L, 21L, 24L, 27L, 30L, 33L, 
36L, 6L, 9L, 12L), .Label = c("1/1/2010", "1/1/2011", "1/1/2012", 
"10/1/2010", "10/1/2011", "10/1/2012", "11/1/2010", "11/1/2011", 
"11/1/2012", "12/1/2010", "12/1/2011", "12/1/2012", "2/1/2010", 
"2/1/2011", "2/1/2012", "3/1/2010", "3/1/2011", "3/1/2012", "4/1/2010", 
"4/1/2011", "4/1/2012", "5/1/2010", "5/1/2011", "5/1/2012", "6/1/2010", 
"6/1/2011", "6/1/2012", "7/1/2010", "7/1/2011", "7/1/2012", "8/1/2010", 
"8/1/2011", "8/1/2012", "9/1/2010", "9/1/2011", "9/1/2012"), class = "factor"), 
    a = c(NA, 365.07, 653.19, 980.72, 1455.6, 1867.07, 2036.92, 
    2372.84, 2693.96, 2973.04, 3227.23, 3678.01, NA, 555.51, 
    1058.18, 1539.01, 2102.23, 2769.65, 3146.88, 3604.71, 4043.18, 
    4438.55, 4860.76, 5360.94, NA, 594.67, 1287.05, 1666.5, 2362.27, 
    2818.16, 3226, 3924.67, 4295.79, 4751.97, 5410.37, 5986.46
    ), b = c(NA, 158.18, 268.53, 331.81, 434.19, 538.49, 606.62, 
    651.46, 736.55, 890.81, 981.65, 1748.44, NA, 227.68, 366.95, 
    486.41, 614.75, 729.44, 836.46, 929.72, 1092.73, 1222.48, 
    1409.07, 2179.42, NA, 172.99, 359.8, 478.05, 597.88, 660.4, 
    823.61, 924.57, 1020.33, 1189.15, 1347.44, 2315.36), ratio_a = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 52.17, 62, 
    56.93, 44.42, 48.34, 54.49, 51.92, 50.08, 49.29, 50.62, 45.76, 
    NA, 7.05, 21.63, 8.28, 12.37, 1.75, 2.51, 8.88, 6.25, 7.06, 
    11.31, 11.67), ratio_b = c(NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, 43.94, 36.65, 46.59, 41.59, 35.46, 37.89, 
    42.71, 48.36, 37.23, 43.54, 24.65, NA, -24.02, -1.95, -1.72, 
    -2.74, -9.46, -1.54, -0.55, -6.63, -2.73, -4.37, 6.24)), class = "data.frame", row.names = c(NA, 
-36L))

出去:

         date        a        b  ratio_a  ratio_b
0    1/1/2010      NaN      NaN      NaN      NaN
1    2/1/2010   365.07   158.18      NaN      NaN
2    3/1/2010   653.19   268.53      NaN      NaN
3    4/1/2010   980.72   331.81      NaN      NaN
4    5/1/2010  1455.60   434.19      NaN      NaN
5    6/1/2010  1867.07   538.49      NaN      NaN
6    7/1/2010  2036.92   606.62      NaN      NaN
7    8/1/2010  2372.84   651.46      NaN      NaN
8    9/1/2010  2693.96   736.55      NaN      NaN
9   10/1/2010  2973.04   890.81      NaN      NaN
10  11/1/2010  3227.23   981.65      NaN      NaN
11  12/1/2010  3678.01  1748.44      NaN      NaN
12   1/1/2011      NaN      NaN      NaN      NaN
13   2/1/2011   555.51   227.68    52.17    43.94
14   3/1/2011  1058.18   366.95    62.00    36.65
15   4/1/2011  1539.01   486.41    56.93    46.59
16   5/1/2011  2102.23   614.75    44.42    41.59
17   6/1/2011  2769.65   729.44    48.34    35.46
18   7/1/2011  3146.88   836.46    54.49    37.89
19   8/1/2011  3604.71   929.72    51.92    42.71
20   9/1/2011  4043.18  1092.73    50.08    48.36
21  10/1/2011  4438.55  1222.48    49.29    37.23
22  11/1/2011  4860.76  1409.07    50.62    43.54
23  12/1/2011  5360.94  2179.42    45.76    24.65
24   1/1/2012      NaN      NaN      NaN      NaN
25   2/1/2012   594.67   172.99     7.05   -24.02
26   3/1/2012  1287.05   359.80    21.63    -1.95
27   4/1/2012  1666.50   478.05     8.28    -1.72
28   5/1/2012  2362.27   597.88    12.37    -2.74
29   6/1/2012  2818.16   660.40     1.75    -9.46
30   7/1/2012  3226.00   823.61     2.51    -1.54
31   8/1/2012  3924.67   924.57     8.88    -0.55
32   9/1/2012  4295.79  1020.33     6.25    -6.63
33  10/1/2012  4751.97  1189.15     7.06    -2.73
34  11/1/2012  5410.37  1347.44    11.31    -4.37
35  12/1/2012  5986.46  2315.36    11.67     6.24

我正在尝试使用下面的代码绘制条形图左侧的 y 轴,a以及线条和点的右侧 y 轴:bratio_aratio_b

library(ggplot2)
library(dplyr)

df$date <- as.Date(df$date, format = "%m/%d/%Y")
df_m <- melt(df, id.vars='date')

df_m_x <- df_m %>% 
  filter(variable %in% c("a", 'b')) 

df_m_ratio_x <- df_m %>% 
  filter(variable %in% c("ratio_a", 'ratio_b')) %>%
  mutate(value = value * 80)
coeff = 1/80

ggplot() +
  geom_bar(data = df_m_x,  aes(x = date, y = value, fill = variable), alpha = 0.6, position = 'dodge', stat = 'identity') +
  geom_line(data = df_m_ratio_x,  aes(x = date, y = value, linetype = variable, col = variable), alpha = 1, size = 1.5) +
  geom_point(data = df_m_ratio_x,  aes(x = date, y = value, col = variable), size = 3) +

  scale_y_continuous(
    name = "㎡&quot;,
    sec.axis = sec_axis(~.*coeff, name = "%")) +
  scale_color_manual(values = c("a" = "#E7B800", "b" = "#FC4E07")) +
  theme(
        legend.title = element_blank(), 
        legend.position = "bottom",
        panel.grid.major = element_line(colour = "grey99"),
        panel.border = element_rect(colour = "grey95", fill=NA),
        panel.background = element_blank(),
        legend.text = element_text(size = 18),
        ) +
  scale_x_date(breaks = date_breaks("6 months"), date_labels = "%Y-%m")

出去:

Removed 6 rows containing missing values (geom_bar).
Removed 72 row(s) containing missing values (geom_path).
Removed 72 rows containing missing values (geom_point).

它适用于条形图,但它不显示和的线和ratio_aratio_b?有谁可以帮我找出为什么会这样?非常感谢。

标签: rggplot2

解决方案


问题是您在scale_color_manual. 而不是你必须使用a,因为这些是数据集中的值。以不同的方式在色标中找不到任何值,因此它们被删除:bratio_a/bvariableggplot2ratio_a/b

library(ggplot2)
library(reshape2)
library(dplyr)
library(scales)

df$date <- as.Date(df$date, format = "%m/%d/%Y")
df_m <- melt(df, id.vars = "date")

df_m_x <- df_m %>%
  filter(variable %in% c("a", "b"))

df_m_ratio_x <- df_m %>%
  filter(variable %in% c("ratio_a", "ratio_b")) %>%
  mutate(value = value * 80)
coeff <- 1 / 80

ggplot() +
  geom_bar(data = df_m_x, aes(x = date, y = value, fill = variable), alpha = 0.6, position = "dodge", stat = "identity") +
  geom_line(data = df_m_ratio_x, aes(x = date, y = value, linetype = variable, col = variable), alpha = 1, size = 1.5) +
  geom_point(data = df_m_ratio_x, aes(x = date, y = value, col = variable), size = 3) +
  scale_y_continuous(
    name = "<U+33A1>",
    sec.axis = sec_axis(~ . * coeff, name = "%")
  ) +
  scale_color_manual(values = c("ratio_a" = "#E7B800", "ratio_b" = "#FC4E07")) +
  theme(
    legend.title = element_blank(),
    legend.position = "bottom",
    panel.grid.major = element_line(colour = "grey99"),
    panel.border = element_rect(colour = "grey95", fill = NA),
    panel.background = element_blank(),
    legend.text = element_text(size = 18),
  ) +
  scale_x_date(breaks = date_breaks("6 months"), date_labels = "%Y-%m")
#> Warning: Removed 6 rows containing missing values (geom_bar).
#> Warning: Removed 26 row(s) containing missing values (geom_path).
#> Warning: Removed 28 rows containing missing values (geom_point).


推荐阅读