如何在R中的条形图上绘制线条 H
也许你想要这个如何在R中的条形图上绘制线条 H,r,bar-chart,R,Bar Chart,也许你想要这个 H <- c(1,2,4,1,0,0,3,1,3) M <- c("one","two","three","four","five") barplot(H,names.arg=M,xlab="number",ylab="random",col="blue", main="bar chart&q
H <- c(1,2,4,1,0,0,3,1,3)
M <- c("one","two","three","four","five")
barplot(H,names.arg=M,xlab="number",ylab="random",col="blue",
main="bar chart",border="blue")
问题中的图表可以用以下代码行绘制: 1。将
x
矢量表化
hist(H, breaks=-1:4, freq=FALSE, xaxt="n")
axis(side=1, at=seq(-0.5, 3.5), labels=M)
lines(density(H))
测试数据创建代码。
ggplot(df1, aes(as.integer(Var1), Freq)) +
geom_bar(stat = "identity", fill = "red", alpha = 0.5) +
geom_smooth(method = stats::loess,
formula = y ~ x,
color = "blue", fill = "blue",
alpha = 0.5)
set.seed(2020年)
f应该M
和H
有相同的长度吗?我知道答案:是的,应该。我不确定你指的是哪条线(平均值?),但你可能想要条形图(表(H),…
。如果你想在平均值处有一条水平线,你可以键入:abline(H=mean(H),lty=2,col=“red”
。或者条形图(表(setNames(H,M)),…)
。我的意思是,在绘制条形图之后,我想添加一条线来显示条形图的形状,一条曲线,我的意思是,你知道我该如何绘制与我显示的图片中相同的形状吗?H
ggplot(df1, aes(as.integer(Var1), Freq)) +
geom_bar(stat = "identity", fill = "red", alpha = 0.5) +
geom_smooth(method = stats::loess,
formula = y ~ x,
color = "blue", fill = "blue",
alpha = 0.5)
set.seed(2020)
f <- function(x) sin(x)^2*exp(x)
p <- f(seq(0, 2.5, by = 0.05))
p <- p/sum(p)
H <- sample(51, size = 1e3, prob = p, replace = TRUE)
library(ggplot2)
library(scales)
Mdate <- as.Date(paste0(M, "/2020"), format = "%d/%m/%Y")
df1 <- data.frame(H, M = Mdate)
ggplot(df1, aes(M, H)) +
geom_bar(stat = "identity", fill = "red", alpha = 0.5) +
geom_smooth(method = stats::loess,
formula = y ~ x,
color = "blue", fill = "blue", alpha = 0.25,
level = 0.5, span = 0.1) +
scale_x_date(labels = date_format("%d/%m"))
H <- c(1,2,4,1,0,0,3,1,3,3, 6,238,0,
58,17,64,38,3,10,8, 10,11,13,
7,25,11,12,13,28,44)
M <- c("29/02","01/03","02/03","03/03",
"04/03","05/03","06/03","07/03",
"08/03", "09/03","10/03","11/03",
"12/03","13/03","14/03","15/03",
"16/03", "17/03","18/03","19/03",
"20/03","21/03","22/03","23/03",
"24/03", "25/03","26/03","27/03",
"28/03","29/03")