同一图表上的多个频率线,其中y是字符值

cro*_*255 6 statistics visualization r graph ggplot2

我正在尝试按年创建图表类型出现次数的频率图.我已经和ggplot2玩了一段时间,但我认为这是我的头脑(我刚刚开始使用R)

我附上了我想要的结果示意图.我遇到的其他问题之一是图表类型多年没有出现.有没有办法在那一年没有出现图表类型时将其排除?

例如在1940年没有"社会图"我不想在0处有一堆线...

year <- c("1940","1940","1940","1940","1940","1940","1940","1940","1940","1940","1940","1941","1941","1941","1941","1941","1941","1941","1941","1941","1941","1941","1941","1941","1941")
type <- c("Line","Column", "Stacked Column", "Scatter with line", "Scatter with line", "Scatter with line", "Scatter with line", "Map with distribution","Line","Line","Line","Bar","Bar","Stacked bar","Column","Column","Sociogram","Sociogram","Column","Column","Column","Line","Line","Line","Line")
ytmatrix <- cbind(as.Date(as.character(year), "%Y", type))
Run Code Online (Sandbox Code Playgroud)

如果事情没有意义,请告诉我.StackOverflow正在迅速成为我最喜欢的网站之一!

谢谢,乔恩


这是我迄今为止所做的工作. 这是我到目前为止所做的...再次感谢您的帮助!

这就是我做的方式(我还不能共享数据文件,因为我们希望将它用于出版物,但ggplot区域可能更有趣,尽管我没有做任何新的事情/帖子中没有讨论过):

AJS = read.csv(data) #read in file
Type = AJS[,17] #select and name "Type" column from csv
Year = AJS[,13] #select and name "Year" column from csv
Year = substr(Year,9,12) #get rid of junk from year column
Year = as.Date(Year, "%Y") #convert the year character to a date
Year = format(Year, "%Y") #get rid of the dummy month and day
Type = as.data.frame(Type) #create data frame
yt <- cbind(Year,Type) #bind the year and type together
library(ggplot2) 

trial <- ggplot(yt, aes(Year,..count.., group= Type)) + #plot the data followed by aes(x-  axis, y-axis, group the lines)
geom_density(alpha = 0.25, aes(fill=Type)) +
opts(axis.text.x = theme_text(angle = 90, hjust = 0)) + #adjust the x axis ticks to horizontal
opts(title = expression("Trends in the Use of Visualizations in The American Journal of Sociology")) + #Add title
scale_y_continuous('Appearances (10 or more)') #change Y-axis label
trial
Run Code Online (Sandbox Code Playgroud)

42-*_*42- 1

这可能是一个更有趣的数据框来进行实验:

df1 <- data.frame(date = as.Date(10*365*rbeta(100, .5, .1)),group="a")
 df2 <- data.frame(date = as.Date(10*365*rbeta(50, .1, .5)),group="b")
 df3 <- data.frame(date = as.Date(10*365*rbeta(25, 3,3)),group="c")
 dfrm <- rbind(df1,df2,df3)
Run Code Online (Sandbox Code Playgroud)

我认为使用帮助(stat_密度)页面中的示例可以工作,但事实并非如此:

m <- ggplot(dfrm, aes(x=date), group=group)
m+ geom_histogram(aes(y=..density..)) + geom_density(fill=NA, colour="black")
Run Code Online (Sandbox Code Playgroud)

然而,我在搜索 hte 档案时发现的一个例子发现 @Hadley Wickham 的帖子确实有效:

m+ geom_density(aes(fill=group), colour="black")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述