2011-10-15 22 views
5

我試圖創建一個按年出現的圖形類型的頻率圖。 我已經玩了一段時間ggplot2,但我認爲這是我的頭(我剛剛開始與R)y在同一圖上的多個頻率線,其中y是字符值

我附上了我想看看結果的原理圖。我遇到的其他問題之一是圖形類型沒有出現很多年。如果圖表類型沒有出現,是否有排除圖表類型的方法?

例如在1940年沒有「sociogram」我不想在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 
+0

這是沒有意義的嘗試堅持一個日期變量成矩陣,即使在您替換缺失的右撇子之後。 –

+0

我不確定這是它不工作的原因... – crock1255

+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) 

我以爲用一個例子工作的幫助( stat_density)頁面可以工作,但它不會:

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

但是我在搜索興田檔案館找到了一個例子發現@Hadley韋翰一個帖子,做工作:

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

enter image description here

+0

謝謝。這真的很有幫助。特別是獲取日期格式。再次感謝你! – crock1255

+0

我看到你發現透明度,並用alpha參數使圖更好。工作很好。 –