我試圖創建一個按年出現的圖形類型的頻率圖。 我已經玩了一段時間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正在迅速成爲我最喜歡的網站之一!
感謝, 喬恩
這裏是我迄今爲止... 再次感謝您對您的幫助!
這就是我是如何做到的(我還無法共享數據文件,因爲我們希望將它用於出版物,但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
這是沒有意義的嘗試堅持一個日期變量成矩陣,即使在您替換缺失的右撇子之後。 –
我不確定這是它不工作的原因... – crock1255
他們現在都是「角色」。試圖計算字符值的密度(似乎是你的目標)可能很困難。 –