R ggplot2中的100%堆积面积图

R ggplot2中的100%堆积面积图,r,csv,ggplot2,R,Csv,Ggplot2,受这个问题的启发,我想用ggplot2创建一个100%堆叠面积图,按国家顺序按年份显示电影。可以检索我的数据帧。我有两个变量年份和国家。我知道我在思考中是否有错误,但我无法找到解决办法 我使用的代码是: library(reshape) library(ggplot2) df <- read.csv(url("https://dl.dropboxusercontent.com/u/109495328/movie_db.csv")) ggplot(df, aes(x=Year,y=Coun

受这个问题的启发,我想用
ggplot2
创建一个100%堆叠面积图,按国家顺序按年份显示电影。可以检索我的数据帧。我有两个变量
年份
国家
。我知道我在思考中是否有错误,但我无法找到解决办法

我使用的代码是:

library(reshape)
library(ggplot2)

df <- read.csv(url("https://dl.dropboxusercontent.com/u/109495328/movie_db.csv"))
ggplot(df, aes(x=Year,y=Country,group=Country,fill=Country)) + geom_area(position="fill")
致:


仍然有点不确定你想要什么,但我的尝试如下:

#load some libraries
library(dplyr)
library(tidyr)

#get rid of some clear errors in your supplied data
df <- filter(df, Country != '')
df <- droplevels(df)

#now pre-calculate the proportion for each country each year summing up to one.
#note that it may be more useful to have actual counts here instead of 0 or 1.
df2 <- table(Year = df$Year, Country = df$Country) %>% prop.table(1) %>% as.data.frame()
#fix year into a numeric
df2$Year <- as.numeric(as.character(df2$Year))

#make the plot
ggplot(df2, aes(x=Year,y=Freq,group=Country,fill=Country)) + 
  geom_area(alpha = 1) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))
#加载一些库
图书馆(dplyr)
图书馆(tidyr)
#消除所提供数据中的一些明显错误

df您缺少一个关于图形应该显示什么的好解释。你的例子有一个连续的y轴,你的代码有一个y因子。同时检查
范围(df$Year)
。在
geom_区域内使用
stat=“identity”
position=“stack”
是否有帮助?请参见@Jaap,它不起作用。它看起来类似于我上面显示的图形输出。@Axeman,我想展示一下,与电影制作国相比,每年有多少部电影在数据框架中。我觉得我必须添加第三个变量,仅在每行中显示
1
。阿克斯曼,你有什么建议吗?阿克斯曼,非常感谢你的尝试!我们很接近!我发现了我的错误:这不是一个100%面积图,而是一个面积图(我相应地调整了标题)。y斧头计划每年放映电影总数。例如,假设2015年的所有电影都有100部应该在那里放映。然而,2014年只有50家。因此,2014年应标明50。我想,
ggplot
检索数字I。E2015年为100,2014年为50,通过读取2015在数据框中出现的次数,并将其保存在您调用的变量
Freq
中。我怎样才能做到呢?对,对不起,你的例子形象把我给甩了。我想您应该改用这一行:
df2%as.data.frame()
(不含
prop.table
)。Axeman,非常感谢您的回复。请注意我上面的编辑。
year country freq
2015 US      6
2015 UK      7
2014 US      10
2014 UK      2
#load some libraries
library(dplyr)
library(tidyr)

#get rid of some clear errors in your supplied data
df <- filter(df, Country != '')
df <- droplevels(df)

#now pre-calculate the proportion for each country each year summing up to one.
#note that it may be more useful to have actual counts here instead of 0 or 1.
df2 <- table(Year = df$Year, Country = df$Country) %>% prop.table(1) %>% as.data.frame()
#fix year into a numeric
df2$Year <- as.numeric(as.character(df2$Year))

#make the plot
ggplot(df2, aes(x=Year,y=Freq,group=Country,fill=Country)) + 
  geom_area(alpha = 1) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))
df3 <- table(Year = df$Year, Country = df$Country) %>% as.data.frame()
#fix year into a numeric
df3$Year <- as.numeric(as.character(df3$Year))

#make the plot
ggplot(df3, aes(x=Year,y=Freq,group=Country,fill=Country)) + 
  geom_area(alpha = 1) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))