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

时间:2011-10-15 16:51:33

标签: r graph statistics ggplot2 visualization

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

我附上了我希望结果如何的示意图。我遇到的其他问题之一是图表类型多年没有出现。如果那一年没有出现图表类型,有没有办法排除它?

e.g。在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))

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

感谢, 乔恩


Here's a working idea of what I have so far. 这是我到目前为止所拥有的...... 再次感谢您的帮助!

这就是我如何做到的(我还不能共享数据文件,因为我们希望将它用于出版物,但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

1 个答案:

答案 0 :(得分: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)

我认为使用help(stat_density)页面中的示例可以正常工作,但事实并非如此:

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

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

m+ geom_density(aes(fill=group), colour="black")

enter image description here