如何在 ggplot2 (R) 的横轴上制作时间直方图
How to make histogram with time on horizontal axis in ggplot2 (R)
我知道如何制作具有常规数值的直方图,但是我希望能够执行类似于以下内容的操作:
我想知道如何在 ggplot2
.
的水平轴上为值 t-1
、t-2
和 t-3
制作直方图
df <- data.frame(trt = c("t-3", "t-2", "t-1", "t"), outcome = c(3, 6, 9, 5))
ggplot(df, aes(trt, outcome)) +
geom_col()
对于那些感兴趣的人,我得到了想要的结果如下:
df <- data.frame(trt = factor(c("t-3", "t-2", "t-1", "t"),
levels = c("t-3", "t-2", "t-1", "t")), outcome = c(9, 3, 7, 4))
myColors <- c("red","red","red", "blue")
u <- (9+3+7)/3
ggplot(df, aes(trt, outcome)) + geom_col(colour = "black",
fill = myColors)+geom_hline(yintercept=u, linetype="dashed",
color = "black") + xlab("Week") + ylab("Search Volume") +
theme_minimal()+theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank())
最后变成了这个样子;
我知道如何制作具有常规数值的直方图,但是我希望能够执行类似于以下内容的操作:
我想知道如何在 ggplot2
.
t-1
、t-2
和 t-3
制作直方图
df <- data.frame(trt = c("t-3", "t-2", "t-1", "t"), outcome = c(3, 6, 9, 5))
ggplot(df, aes(trt, outcome)) +
geom_col()
对于那些感兴趣的人,我得到了想要的结果如下:
df <- data.frame(trt = factor(c("t-3", "t-2", "t-1", "t"),
levels = c("t-3", "t-2", "t-1", "t")), outcome = c(9, 3, 7, 4))
myColors <- c("red","red","red", "blue")
u <- (9+3+7)/3
ggplot(df, aes(trt, outcome)) + geom_col(colour = "black",
fill = myColors)+geom_hline(yintercept=u, linetype="dashed",
color = "black") + xlab("Week") + ylab("Search Volume") +
theme_minimal()+theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank())
最后变成了这个样子;