R:使用数据帧的平均值创建绘图
我是R的新手 我在第3列到第6列的数据框中有一些值,我想在点图中绘制这些值。第3列到第6列各表示一个月,第1行到第30行表示月份中的某一天。数据框内的数字表示温度 我想画一个图,在这里,y轴上有温度,x轴上有月份。然后,在图上有点代表每个温度,有一条线穿过,在这里你可以跟踪每个月的平均温度 但是有些温度是相同的,所以我想给其中一个加上一个非常小的值,这样你可以在最普通的温度下看到很多点 我试过:R:使用数据帧的平均值创建绘图,r,plot,R,Plot,我是R的新手 我在第3列到第6列的数据框中有一些值,我想在点图中绘制这些值。第3列到第6列各表示一个月,第1行到第30行表示月份中的某一天。数据框内的数字表示温度 我想画一个图,在这里,y轴上有温度,x轴上有月份。然后,在图上有点代表每个温度,有一条线穿过,在这里你可以跟踪每个月的平均温度 但是有些温度是相同的,所以我想给其中一个加上一个非常小的值,这样你可以在最普通的温度下看到很多点 我试过: boxplot(dat3[,3:6],dat3=mean, geom="point", shape=
boxplot(dat3[,3:6],dat3=mean, geom="point", shape=18,
size=3, color="red")
然而,这并没有在平均值和以条形图的形式绘制温度之间形成一条直线。我只想要点和平均值之间的一条线
有可能吗
谢谢大家。我制作了一个很小的(不真实的)数据框,但你们可以合并你们自己的数据
structure(list(Month = structure(1:4, .Label = c("April", "May",
"June", "July"), class = "factor"), X1 = c(50, 55, 57, 68), X2 = c(60,
66, 68.4, 81.6), X3 = c(65, 71.5, 74.1, 88.4), X4 = c(40, 44,
45.6, 54.4), X5 = c(50, 55, 57, 68), X6 = c(60, 66, 68.4, 81.6
), X7 = c(65, 71.5, 74.1, 88.4), X8 = c(40, 44, 45.6, 54.4),
X9 = c(50, 55, 57, 68), X10 = c(60, 66, 68.4, 81.6), X11 = c(65,
71.5, 74.1, 88.4), X12 = c(40, 44, 45.6, 54.4), X13 = c(50,
55, 57, 68), X14 = c(60, 66, 68.4, 81.6), X15 = c(65, 71.5,
74.1, 88.4), X16 = c(40, 44, 45.6, 54.4), X17 = c(50, 55,
57, 68), X18 = c(60, 66, 68.4, 81.6), X19 = c(65, 71.5, 74.1,
88.4), X20 = c(40, 44, 45.6, 54.4), X21 = c(50, 55, 57, 68
), X22 = c(60, 66, 68.4, 81.6), X23 = c(65, 71.5, 74.1, 88.4
), X24 = c(40, 44, 45.6, 54.4), X25 = c(50, 55, 57, 68),
X26 = c(60, 66, 68.4, 81.6), X27 = c(65, 71.5, 74.1, 88.4
), X28 = c(40, 44, 45.6, 54.4), X29 = c(50, 55, 57, 68),
X30 = c(50, 55, 57, 68)), .Names = c("Month", "X1", "X2",
"X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10", "X11", "X12",
"X13", "X14", "X15", "X16", "X17", "X18", "X19", "X20", "X21",
"X22", "X23", "X24", "X25", "X26", "X27", "X28", "X29", "X30"
), row.names = c(NA, -4L), class = "data.frame")
经过一些清理工作后,有几种方法可以绘制数据,但这里有一种:
library(dplyr)
df$Month <- factor(df$Month, levels = c("April", "May", "June", "July")) # changed the order from alphabetical
df.m <- melt(df, id.vars = "Month") # melted the data frame into long format
df.m$variable <- str_replace_all(string = df.m$variable, pattern = "X", replacement = "") # remove the X before dates
avg.temp <- df.m %>% group_by(Month) %>% summarise(avg = mean(value)) # calculated the monthly mean for plotting
library(ggplot2)
ggplot(df.m, aes(x = factor(variable), y = value)) +
geom_point() +
geom_point(data = avg.temp, aes(x = 15, y = avg), size = 7, color = "red") +
facet_wrap(~Month) +
theme_bw() +
labs(x = "Days of the Month", y = "Temperature (F)", title = "Distribution of Temperatures -- Monthly Mean in Red")
库(dplyr)
df$Month使用ggplot2(用于打印)、tidyr(用于将表格转换为更易于处理的数据框)和dplyr(用于使用数据框)的解决方案:
df%采集(月份、温度)
df.avg%组(月)%>%总结(平均值=平均值(温度))
ggplot()+
几何点(数据=df.temps,aes(x=温度,y=月份),位置=位置抖动(宽度=1,高度=0))+
几何点(数据=df.avg,aes(x=平均值,y=月),颜色=“红色”,大小=3)+
geom_线(数据=df.avg,aes(x=平均值,y=月,组=NA))+
实验室(x=“温度(华氏度)”,y=“月份”)
您能为我们提供一个小数据集吗?为了快速绘制数据帧,我建议使用ggplot2。它包括绘制散点图、线图和组合图的功能,以及添加抖动和计算平均值的功能。哇,这是一个既酷又令人印象深刻的答案!有没有可能像点图一样,月份在x轴上,不同的温度在y轴上。所有的日子都只属于一个月,所以我可以在平均数之间划出一条线。是的,这当然是可能的。你为什么不把我的代码和你的数据用在这方面呢?因此,这不是编码服务;我们试图回答人们提交的特定编码问题。顺便说一下,如果这回答了你的问题,即使不是你希望的进一步改进,也可以考虑通过点击接受箭头来接受。
df <- structure(list(Jan = c(50L, 60L, 65L, 40L, 50L, 60L, 65L, 40L,
50L, 60L, 65L, 40L, 50L, 60L, 65L, 40L, 50L, 60L, 65L, 40L, 50L,
60L, 65L, 40L, 50L, 60L, 65L, 40L, 50L, 50L), Feb = c(50L, 60L,
65L, 40L, 50L, 60L, 65L, 40L, 50L, 60L, 65L, 40L, 50L, 60L, 65L,
40L, 50L, 60L, 65L, 40L, 50L, 60L, 65L, 40L, 50L, 60L, 65L, 40L,
50L, 50L), Mar = c(50L, 60L, 65L, 40L, 50L, 60L, 65L, 40L, 50L,
60L, 65L, 40L, 50L, 60L, 65L, 40L, 50L, 60L, 65L, 40L, 50L, 60L,
65L, 40L, 50L, 60L, 65L, 40L, 50L, 50L), Apr = c(50L, 60L, 65L,
40L, 50L, 60L, 65L, 40L, 50L, 60L, 65L, 40L, 50L, 60L, 65L, 40L,
50L, 60L, 65L, 40L, 50L, 60L, 65L, 40L, 50L, 60L, 65L, 40L, 50L,
50L), May = c(50L, 60L, 65L, 40L, 50L, 60L, 65L, 40L, 50L, 60L,
65L, 40L, 50L, 60L, 65L, 40L, 50L, 60L, 65L, 40L, 50L, 60L, 65L,
40L, 50L, 60L, 65L, 40L, 50L, 50L), Jun = c(55L, 66L, 71L, 44L,
55L, 66L, 71L, 44L, 55L, 66L, 71L, 44L, 55L, 66L, 71L, 44L, 55L,
66L, 71L, 44L, 55L, 66L, 71L, 44L, 55L, 66L, 71L, 44L, 55L, 55L
), Jul = c(57L, 68L, 74L, 45L, 57L, 68L, 74L, 45L, 57L, 68L,
74L, 45L, 57L, 68L, 74L, 45L, 57L, 68L, 74L, 45L, 57L, 68L, 74L,
45L, 57L, 68L, 74L, 45L, 57L, 57L), Aug = c(68L, 81L, 88L, 54L,
68L, 81L, 88L, 54L, 68L, 81L, 88L, 54L, 68L, 81L, 88L, 54L, 68L,
81L, 88L, 54L, 68L, 81L, 88L, 54L, 68L, 81L, 88L, 54L, 68L, 68L
)), .Names = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug"), class = "data.frame", row.names = c(NA, -30L))
library(ggplot2)
library(tidyr)
library(dplyr)
df.temps <- df %>% select(Mar:Jun) %>% gather(month, temperature)
df.avg <- df.temps %>% group_by(month) %>% summarise(average=mean(temperature))
ggplot() +
geom_point(data=df.temps, aes(x=temperature, y=month), position=position_jitter(width=1, height=0)) +
geom_point(data=df.avg, aes(x=average, y=month), color="red", size=3) +
geom_line(data=df.avg, aes(x=average, y=month, group=NA)) +
labs(x = "Temperature (in F)", y = "Month")