我有一个包含以下信息的数据:
Year Total Population UK EEA NON-EEA
2007 60510 54102 1999 4409
2008 60995 54225 2154 4615
2009 61437 54415 2235 4787
2010 61933 54699 2331 4903
2011 62448 54787 2580 5080
2012 62864 55042 2671 5151
2013 63230 55309 2762 5160
2014 63653 55375 3042 5236
2015 64212 55642 3204 5365我如何创建一个图表,在图中我可以看到每年总人口的变化,但同时也显示了英国、欧洲经济区和非EEA人口,因为当我们对所有三个变量进行总结时,我们得到了来自“总人口”栏的数据。
此外,我还使用了以下代码:
dat <- read.table(text = "Total_Population United_Kingdom EEA Non_EEA
2007 60510 54102 1999 4409
2008 60995 54225 2154 4615
2009 61437 54415 2235 4787
2010 61933 54699 2331 4903
2011 62448 54787 2580 5080
2012 62864 55042 2671 5151
2013 63230 55309 2762 5160
2014 63653 55375 3042 5236
2015 64212 55642 3204 5365", header = TRUE)
library(reshape2)
dat$row <- seq_len(nrow(dat))
dat2 <- melt(dat, id.vars = "row")
library(ggplot2)
ggplot(dat2, aes(x = variable, y = value, fill = row)) +
geom_bar(stat = "identity") +
xlab("\nCountry of Birth") +
ylab("Population\n") +
guides(fill = FALSE) +
theme_bw()它给了我酒保,但我想要更复杂的东西。
发布于 2019-05-06 10:04:39
我们可以利用以下几点:
library(dplyr)
library(ggplot2)
library(tidyr)
dat %>%
mutate(Year=row.names(.)) %>%
gather(key,value,-c(Year,Total_Population)) %>%
ggplot(aes(Year,Total_Population,fill=key))+
geom_bar(stat="identity")另一种选择是:
dat %>%
mutate(Year=row.names(.)) %>%
gather(key,value,-c(Year,Total_Population)) %>%
ggplot(aes(Year,value,fill=key))+
geom_bar(stat="identity",position="dodge")数据
dat<-structure(list(Total_Population = c(60510L, 60995L, 61437L, 61933L,
62448L, 62864L, 63230L, 63653L, 64212L), United_Kingdom = c(54102L,
54225L, 54415L, 54699L, 54787L, 55042L, 55309L, 55375L, 55642L
), EEA = c(1999L, 2154L, 2235L, 2331L, 2580L, 2671L, 2762L, 3042L,
3204L), Non_EEA = c(4409L, 4615L, 4787L, 4903L, 5080L, 5151L,
5160L, 5236L, 5365L)), class = "data.frame", row.names = c("2007",
"2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015"
))https://stackoverflow.com/questions/56001714
复制相似问题