我试图为不同州的销售额绘制一个线图,但无论我从previously问题中尝试什么解决方案,都只能得到锯齿形。
ggplot(df, aes(x = year, y = sales, group = location, color = location)) +
geom_line() +
theme(plot.title = element_text(hjust = 0.5))
所以我真的很感激,如果你能帮我发现这里的问题。请看下面的数据。
location age year sales
1 South-East Asia Region <20 years 2002 0.01
2 Western Sub-Saharan Africa 20-24 years 2010 0.04
3 Commonwealth Middle Income 40-44 years 2003 0.18
4 Eastern Europe 45-49 years 2008 0.37
5 World Bank Lower Middle Income <20 years 2005 0.01
6 Oceania 45-49 years 2006 0.26
7 Commonwealth Middle Income 55-59 years 2004 0.30
8 Western Sub-Saharan Africa 30-34 years 1997 0.04
9 High-income Asia Pacific 65-74 years 2000 0.24
10 Caribbean 55-59 years 2008 0.33
11 Andean Latin America 75+ years 2019 0.28
12 Eastern Europe 40-44 years 1998 0.25
13 North Africa and Middle East 30-34 years 2009 0.18
14 Eastern Europe <20 years 2015 0.02
15 Western Pacific Region 25-29 years 2001 0.11
16 Sub-Saharan Africa - WB 20-24 years 2012 0.04
17 Africa 20-24 years 2002 0.03
18 Western Sub-Saharan Africa 55-59 years 1997 0.20
19 Southern Sub-Saharan Africa 50-54 years 2014 0.12
20 Asia 45-49 years 2019 0.30
21 World Bank High Income 65-74 years 2000 0.35
22 African Region All ages 1992 0.10
23 Commonwealth Middle Income <20 years 1993 0.01
24 Western Sub-Saharan Africa 50-54 years 1993 0.19
25 Western Europe 75+ years 1993 0.53
26 Central Europe 25-29 years 2013 0.09
27 South-East Asia Region 40-44 years 2014 0.23
28 Sub-Saharan Africa - WB 55-59 years 2006 0.18
29 Commonwealth Low Income All ages 1999 0.11
30 Eastern Sub-Saharan Africa 30-34 years 2010 0.02
31 Oceania 25-29 years 2019 0.14
32 Caribbean 40-44 years 2003 0.16
33 European Region 55-59 years 2002 0.37
34 Central Sub-Saharan Africa 80+ years 2013 0.41
35 World Bank Upper Middle Income 20-24 years 1996 0.09
36 Southern Latin America 55-59 years 2005 0.30
37 America All ages 1994 0.32
38 World Bank Low Income <20 years 1993 0.01
39 Southern Latin America 65-74 years 1998 0.35
40 World Bank Upper Middle Income 60-64 years 1998 0.39
41 Eastern Mediterranean Region 35-39 years 2006 0.22
42 European Region 35-39 years 2008 0.21
43 South Asia 80+ years 2010 0.32
44 Sub-Saharan Africa - WB 50-54 years 2001 0.16
45 Andean Latin America 55-59 years 1993 0.22
46 Tropical Latin America 45-49 years 2005 0.30
47 Sub-Saharan Africa - WB 50-54 years 2002 0.13
48 World Bank High Income 40-44 years 2007 0.20
49 North Africa and Middle East 25-29 years 2011 0.11
50 Eastern Mediterranean Region 65-74 years 1993 0.48
2条答案
按热度按时间wz3gfoph1#
group()
只能用于year
或location
,这会导致year
包含不同locations
中的pct
值。例如:2002年的数据包括东南亚地区的0.01和撒哈拉以南非洲----世界银行的0.13。
如果方便的话,您可以提供您的
ggplot2
代码吗?我用
mtcars
做了一个小测试,其中已经考虑了mtcars的gear
和carb
。这对您有帮助吗?
zbdgwd5y2#
有了你的数据
与其他注解和答案一样,绘制线图时应指定
group
参数。创建于2023年2月23日,使用reprex v2.0.2