R:定义两个要素的ggplot方框图的轴
我有一个小型数据集,以EJ(Exajoules)表示煤炭和可再生能源的年能耗:R:定义两个要素的ggplot方框图的轴,r,ggplot2,boxplot,R,Ggplot2,Boxplot,我有一个小型数据集,以EJ(Exajoules)表示煤炭和可再生能源的年能耗: Year Coal_cons Renewable_cons 1 1965 58.10 0.21 2 1966 58.77 0.23 3 1967 57.82 0.24 4 1968 58.68 0.26 5 1969 60.48 0.27 6 1970
Year Coal_cons Renewable_cons
1 1965 58.10 0.21
2 1966 58.77 0.23
3 1967 57.82 0.24
4 1968 58.68 0.26
5 1969 60.48 0.27
6 1970 61.41 0.30
7 1971 61.08 0.32
8 1972 61.76 0.35
9 1973 63.60 0.37
10 1974 63.66 0.39
11 1975 64.89 0.39
12 1976 67.28 0.43
13 1977 69.27 0.47
14 1978 70.05 0.51
15 1979 73.31 0.56
16 1980 75.09 0.60
17 1981 76.14 0.63
18 1982 76.98 0.74
19 1983 79.36 0.84
20 1984 82.80 0.96
21 1985 86.35 1.01
22 1986 87.32 1.15
23 1987 90.76 1.21
24 1988 93.47 1.26
25 1989 94.37 1.40
26 1990 93.22 1.58
27 1991 92.31 1.66
28 1992 91.98 1.75
29 1993 92.43 1.82
30 1994 92.78 1.91
31 1995 93.44 2.01
32 1996 95.66 2.04
33 1997 95.50 2.20
34 1998 94.89 2.31
35 1999 95.48 2.46
36 2000 98.70 2.64
37 2001 100.27 2.75
38 2002 104.20 3.10
39 2003 113.39 3.37
40 2004 121.19 3.87
41 2005 130.21 4.37
42 2006 137.05 5.03
43 2007 144.80 5.94
44 2008 146.77 7.16
45 2009 144.53 8.24
46 2010 151.19 9.70
47 2011 158.46 11.12
48 2012 159.07 12.60
49 2013 161.98 14.36
50 2014 161.84 16.03
51 2015 157.84 18.08
52 2016 155.50 20.06
53 2017 156.09 23.04
54 2018 158.79 25.83
55 2019 157.86 28.98
我想实现下面的代码,以便实现与图片类似的输出:
但是,我不清楚如何定义各个列中的轴
如何设置轴以获得与图片上的轴类似的输出?这里是一个
tidyverse
解决方案
library(tidyverse)
df %>%
pivot_longer(2:3) %>% # pivot_longer to one column for names and one for values
ggplot(aes(x = name, y = value, color = name)) +
geom_boxplot() +
geom_point(position = position_jitter(width = 0.3)) # position_jitter for the dots
这里有一种使用facet的替代方法,因此第二个箱线图更容易阅读。您将失去直接比较y刻度的能力
df %>%
pivot_longer(2:3) %>%
ggplot(aes(x = name, y = value, color = name)) +
geom_boxplot() +
geom_point(position = position_jitter(width = 0.3)) +
facet_wrap(~name, scales = "free")
这是数据的dput()
structure(list(Year = c(1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973,
1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982,
1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991,
1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
2019),
Coal_cons = c(58.1, 58.77, 57.82, 58.68, 60.48, 61.41, 61.08,
61.76, 63.6, 63.66, 64.89, 67.28, 69.27, 70.05,
73.31, 75.09, 76.14, 76.98, 79.36, 82.8, 86.35,
87.32, 90.76, 93.47, 94.37, 93.22, 92.31, 91.98,
92.43, 92.78, 93.44, 95.66, 95.5, 94.89, 95.48,
98.7, 100.27, 104.2, 113.39, 121.19, 130.21,
137.05, 144.8, 146.77, 144.53, 151.19, 158.46,
159.07, 161.98, 161.84, 157.84, 155.5, 156.09,
158.79, 157.86),
Renewable_cons = c(0.21, 0.23, 0.24, 0.26, 0.27, 0.3, 0.32,
0.35, 0.37, 0.39, 0.39, 0.43, 0.47, 0.51,
0.56, 0.6, 0.63, 0.74, 0.84, 0.96, 1.01,
1.15, 1.21, 1.26, 1.4, 1.58, 1.66, 1.75,
1.82, 1.91, 2.01, 2.04, 2.2, 2.31, 2.46,
2.64, 2.75, 3.1, 3.37, 3.87, 4.37, 5.03,
5.94, 7.16, 8.24, 9.7, 11.12, 12.6, 14.36,
16.03, 18.08, 20.06, 23.04, 25.83, 28.98)),
class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
row.names = c(NA, -55L))
因此,你想展示的不是奥巴马和特朗普,而是煤炭和可再生能源!我只是有一个困难的时间知道如何定义轴,或者即使这是最好的绘图使用考虑到我有每年的数据。。。你的建议是什么?我得到了这个错误:UseMethod中的错误(“pivot_longer”):没有适用于“function”@Joehat类对象的“pivot_longer”方法,这可能是由df
df
引起的,df也是一个函数。请将数据重命名为df1
,然后重试。可能这不是比较此数据的最佳绘图。你建议我怎么做?这是一个很好的情节,取决于你想要传达什么。您是否希望告知,与可再生能源相比,coal\u cons
往往更大且范围更广?这是一个很好的阴谋。我添加了一个备用图,将它们分隔为具有独立y轴的面板。您可以更轻松地阅读可更新内容
箱线图,但比较这两个类别比较困难。