R语言 为什么线图(ggplot2)产生垂直线?

k10s72fa  于 2023-02-26  发布在  其他
关注(0)|答案(2)|浏览(205)

我试图为不同州的销售额绘制一个线图,但无论我从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

wz3gfoph

wz3gfoph1#

group()只能用于yearlocation,这会导致year包含不同locations中的pct值。
例如:2002年的数据包括东南亚地区的0.01和撒哈拉以南非洲----世界银行的0.13。
如果方便的话,您可以提供您的ggplot2代码吗?
我用mtcars做了一个小测试,其中已经考虑了mtcars的gearcarb

mt %>% 
group_by(gear,carb) %>% 
mutate(mean=mean(disp)) %>% 
ggplot(aes(gear,mean,group=carb,color=carb))
+geom_line()+geom_point()


这对您有帮助吗?

zbdgwd5y

zbdgwd5y2#

有了你的数据

df <- data.frame(
  location = c("South-East Asia Region","Western Sub-Saharan Africa",
    "Commonwealth Middle Income","Eastern Europe","World Bank Lower Middle Income",
    "Oceania","Commonwealth Middle Income"   ,"Western Sub-Saharan Africa"   ,"High-income Asia Pacific"   ,
    "Caribbean"   ,"Andean Latin America","Eastern Europe"  ,"North Africa and Middle East"   ,
    "Eastern Europe"   ,"Western Pacific Region","Sub-Saharan Africa - WB","Africa",
    "Western Sub-Saharan Africa"   ,"Southern Sub-Saharan Africa"   ,"Asia"   ,"World Bank High Income"   ,
    "African Region" ,"Commonwealth Middle Income"   ,"Western Sub-Saharan Africa","Western Europe" ,
    "Central Europe"   ,"South-East Asia Region"   ,"Sub-Saharan Africa - WB"   ,"Commonwealth Low Income" ,
    "Eastern Sub-Saharan Africa","Oceania"   ,"Caribbean"   ,"European Region"   ,
    "Central Sub-Saharan Africa" ,"World Bank Upper Middle Income"   ,"Southern Latin America"   ,"America" ,
    "World Bank Low Income"   ,"Southern Latin America"   ,"World Bank Upper Middle Income","Eastern Mediterranean Region"   ,
    "European Region"   ,"South Asia","Sub-Saharan Africa - WB"   ,"Andean Latin America"   ,"Tropical Latin America"   ,
    "Sub-Saharan Africa - WB"   ,"World Bank High Income"   ,"North Africa and Middle East"   ,"Eastern Mediterranean Region"
  ),
  age = c(
    "<20 years", "20-24 years","40-44 years","45-49 years","<20 years",
    "45-49 years","55-59 years","30-34 years","65-74 years","55-59 years",
    "75+ years","40-44 years","30-34 years","<20 years","25-29 years",
    "20-24 years","20-24 years","55-59 years","50-54 years","45-49 years",
    "65-74 years","All ages"  ,"<20 years","50-54 years","75+ years",
    "25-29 years","40-44 years","55-59 years","All ages"  ,"30-34 years",
    "25-29 years","40-44 years","55-59 years","80+ years"  ,"20-24 years",
    "55-59 years","All ages","<20 years","65-74 years","60-64 years",
    "35-39 years","35-39 years","80+ years"  ,"50-54 years","55-59 years",
    "45-49 years","50-54 years","40-44 years","25-29 years","65-74 years"
  ),
  year = c(2002,2010,2003,2008,2005,2006,2004,1997,2000,2008,
            2019,1998,2009,2015,2001,2012,2002,1997,2014,2019,
            2000,1992,1993,1993,1993,2013,2014,2006,1999,2010,
            2019,2003,2002,2013,1996,2005,1994,1993,1998,1998,
            2006,2008,2010,2001,1993,2005,2002,2007,2011,1993),
  sales = c(0.01,0.04,0.18,0.37,0.01,0.26,0.30,0.04,0.24,0.33,0.28,
             0.25,0.18,0.02,0.11,0.04,0.03,0.20,0.12,0.30,0.35,0.10,
             0.01,0.19,0.53,0.09,0.23,0.18,0.11,0.02,0.14,0.16,0.37,
             0.41,0.09,0.30,0.32,0.01,0.35,0.39,0.22,0.21,0.32,0.16,
             0.22,0.30,0.13,0.20,0.11,0.4)
)
require(ggplot2)
#> Loading required package: ggplot2
#> Warning: package 'ggplot2' was built under R version 4.2.2
df |> 
  ggplot(aes(x=year, y= sales, group=location,
             color=location)) +
  geom_line() +
  scale_x_continuous(breaks=seq(1992,2020,1)) +
  theme(axis.text.x = element_text(angle=90))

与其他注解和答案一样,绘制线图时应指定group参数。

创建于2023年2月23日,使用reprex v2.0.2

相关问题