无法在条形图中添加线性回归线 [英] Fail to add linear regression line in barplot
问题描述
我有一些有关不同时间段的温度百分比的数据,我想创建一个显示这些百分比的条形图,然后添加一个显示趋势的线性回归线.尽管我设法获得了第一张图,但我无法添加直线回归直线
I have some data about the percentages of temperature for different time periods and I want to create a barplot showing those percentages and then add a linear regression line showing the trend. Although i manage to get the first graph, I fail to add a straight linear regression line
基本上,我尝试使用这些tx_1数据制作一个条形图
Basically I try to make a barplot with these tx_1 data
tx_1< -c(0.055,0.051,0.057,0.049,0.061,0.045)
mypath<-file.path("C:\\tx5\\1.jpeg")
jpeg(file = mypath,width = 1200, height = 600)
plot.dim<-barplot(get(name),
space= 2,
ylim=c(0,0.15),
main = "Percentage of days when Tmax < 5th percentile",
xlab = "Time Periods",
ylab = "Percentage",
names.arg = c("1975-1984", "1985-1990", "1991-1996", "1997-2002", "2003-2008", "2009-2014"),
col = "darkred",
horiz = FALSE)
dev.off()
我也尝试使用ggplot,但是没有运气
I tried using ggplot also, but with no luck
推荐答案
在这里,我同时包含了连接每个观察值的线和总体最佳线性拟合线.希望这会有所帮助.
Here i have included both a line connecting each observation and a overall best linear fit line. Hope this helps.
library(tidyverse)
year <- tribble(~ Year,~ Percent,
94,0.055,
95,0.051,
96,0.057,
97,0.049,
98,0.061,
99,0.045)
ggplot(year,aes(Year,Percent)) +
geom_bar(stat = "identity") +
geom_line() +
geom_smooth(method = "lm",se = F)
这篇关于无法在条形图中添加线性回归线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!