如何在ggplot2(R)中绘制水平轴上的时间直方图
我知道如何使用常规数值制作柱状图,但我希望能够执行以下类似操作: 我想知道如何为如何在ggplot2(R)中绘制水平轴上的时间直方图,r,ggplot2,histogram,figure,R,Ggplot2,Histogram,Figure,我知道如何使用常规数值制作柱状图,但我希望能够执行以下类似操作: 我想知道如何为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对于那些感兴趣的人,我得到了如下期望的结果: df <- data.frame(trt = fa
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对于那些感兴趣的人,我得到了如下期望的结果:
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())
df您能否向我们展示您最初的工作并指出您遇到的问题?请提供一个。我添加了代码,我到目前为止,但还不满意它。特别是,我想让RHS上的列离其他三个列稍微远一点。您的trt
值不是次,而是字符串。直方图表示连续值的分布。您可以将trt
更改为数字值,其中每个值可能相差1,除了t
与最近的值相差1.5或类似的值,以将其移开。如果将处理顺序定义为因子,则可以更改处理顺序。然后,它将是所使用级别的第四个顺序:trt=factor(c(“t-3”、“t-2”、“t-1”、“t”)),级别=c(“t-3”、“t-2”、“t-1”、“t”))
。谢谢你,它成功了!