gpt4 book ai didi

r - 同一图上的多条频率线,其中 y 是字符值

转载 作者:行者123 更新时间:2023-12-04 16:01:01 25 4
gpt4 key购买 nike

我正在尝试按年份创建图形类型出现次数的频率图。
我已经玩了一段时间的 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))

如果有什么不合理的地方,请告诉我。 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

最佳答案

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

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

关于r - 同一图上的多条频率线,其中 y 是字符值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7779226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com