简体   繁体   中英

2 continuous variables with a categorical variable

Not sure what graph to use.

I currently have two density plots one for each continuous variable Vul and Glob against the categorical variable Continent which can be seen in dataframe below.

a = ggplot(Data,aes(x = Glob, fill = Continent)) + geom_density(alpha = 0.4) +labs(title = "Globalisation by Continent")

b = ggplot(Data,aes(x = Vul, fill = Continent)) + geom_density(alpha = 0.4) +labs(title = "Vulnerability by Continent")

grid.arrange(a,b)
 Country       Vul     Glob        Emm
1         Afghanistan 0.5963081 38.11748   9809.225
2             Albania 0.4227595 67.62799   5716.853
3             Algeria 0.3698284 56.07350 145400.217
4              Angola 0.5220609 43.27245  34763.160
5 Antigua and Barbuda 0.4864335 58.04864    531.715
6           Argentina 0.3681622 65.22942 204024.546
     EMMPC              GDPpc1 Code             GDPpc
1 0.299445 625.3395388+FF2:F55  AFG under-performing 
2 1.978763         4578.667934  ALB        performing
3 3.717410         5466.425778  DZA        performing
4 1.291328         5412.692348  AGO        performing
5 5.377649           12900.903  ATG        performing
6 4.746797         12245.25645  ARG        performing
      Continent  Latitude  AvgTemp Coastline ABSLatitude
1          Asia  33.83523 13.64797         0    33.83523
2        Europe  41.14245 12.70404         1    41.14245
3        Africa  28.15894 23.87725         1    28.15894
4        Africa -12.29336 21.97892         1    12.29336
5 North America  17.27750 26.27178         2    17.27750
6 South America -35.38135 14.84694         1    35.38135
  Coastline1 GDPPCLM  GDPpc000s
1          0       0  0.6253395
2          1       0  4.5786679
3          1       0  5.4664258
4          1       0  5.4126923
5          1       1 12.9009030
6          1       1 12.2452564

I would like one graph that shows both density plots or one graph to represent both continuous variables Vul and Glob against Continent.

I think facet_grid() would work very well in this case. But to generate graphs using ggplot (and facet_grid() in this case), making the original data frame tidy format would be really convenient (as suggested by @eipi10 as well).

 tidy_Data = Data %>% 
  gather(key, value, Vul, Glob)
 tidy_Data
  ggplot(aes(value)) +
  geom_density(aes(fill = key), show.legend = FALSE) +
  facet_grid(vars(Continent), vars(key), scales = "free")

I hope you find this helpful. If you want to know more about the function I suggest you check out this official document .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM