需要使用data.frame中的两列数据制作条形图
我正在尝试从一个data.frame并排创建两列的条形图。我试过:需要使用data.frame中的两列数据制作条形图,r,R,我正在尝试从一个data.frame并排创建两列的条形图。我试过: barplot(data.frame$data1, data.frame$data2, data=data.frame) here is data: Neutral Emotional 1 0.790 1.6400 2 0.051 0.0880 3 0.891 2.7200 4 0.430 1.1800 5 -0.009 -0.6000 但它制造了大量的酒吧,而不仅仅是两个。
barplot(data.frame$data1, data.frame$data2, data=data.frame)
here is data:
Neutral Emotional
1 0.790 1.6400
2 0.051 0.0880
3 0.891 2.7200
4 0.430 1.1800
5 -0.009 -0.6000
但它制造了大量的酒吧,而不仅仅是两个。我尝试使用两个条,一个是中性条,一个是情绪条和错误条,代表SEM。一个选项是将
收集成“长”格式,然后从ggplot2
使用几何条
library(tidyverse)
library(ggplot2)
gather(df1) %>%
ggplot(., aes(x = key, y = value)) +
geom_bar(stat = 'identity')
如果我们还需要一个错误条,那么
gather(df1) %>%
ggplot(., aes(x = key, y = value)) +
stat_summary(fun.y = mean, geom = "bar") +
stat_summary(fun.data = mean_se, geom = "errorbar")
数据
df1一个选项是将收集成“长”格式,然后从ggplot2
library(tidyverse)
library(ggplot2)
gather(df1) %>%
ggplot(., aes(x = key, y = value)) +
geom_bar(stat = 'identity')
如果我们还需要一个错误条,那么
gather(df1) %>%
ggplot(., aes(x = key, y = value)) +
stat_summary(fun.y = mean, geom = "bar") +
stat_summary(fun.data = mean_se, geom = "errorbar")
数据
讨论了实现这一结果的方法。请注意,他们建议将ggplot2
置于barplot
之上
要获取包含平均值标准误差的误差条的图表:
library(tidyverse)
data.frame %>%
gather(Var, Val) %>%
group_by(Var) %>%
summarise(Mean = mean(Val),
SD = sd(Val),
SE = SD/sqrt(n())) %>%
ggplot(aes(Var, Mean)) +
geom_col() +
geom_errorbar(aes(ymin = Mean - SE,
ymax = Mean + SE),
width = 0.5)
结果:
然而:请注意,数据可视化专家并不看好所谓的“炸药图”。对于少量样本,最好使用geom\u箱线图
或geom\u抖动
显示范围
箱线图:
data.frame %>%
gather(Var, Val) %>%
ggplot(aes(Var, Val)) +
geom_boxplot()
平均抖动:
data.frame %>%
gather(Var, Val) %>%
ggplot(aes(Var, Val)) +
geom_jitter(width = 0.2) +
stat_summary(geom = "crossbar",
fun.y = mean,
fun.ymax = mean,
fun.ymin = mean,
color = "red",
width = 0.4)
讨论了实现这一结果的方法。请注意,他们建议将ggplot2
置于barplot
之上
要获取包含平均值标准误差的误差条的图表:
library(tidyverse)
data.frame %>%
gather(Var, Val) %>%
group_by(Var) %>%
summarise(Mean = mean(Val),
SD = sd(Val),
SE = SD/sqrt(n())) %>%
ggplot(aes(Var, Mean)) +
geom_col() +
geom_errorbar(aes(ymin = Mean - SE,
ymax = Mean + SE),
width = 0.5)
结果:
然而:请注意,数据可视化专家并不看好所谓的“炸药图”。对于少量样本,最好使用geom\u箱线图
或geom\u抖动
显示范围
箱线图:
data.frame %>%
gather(Var, Val) %>%
ggplot(aes(Var, Val)) +
geom_boxplot()
平均抖动:
data.frame %>%
gather(Var, Val) %>%
ggplot(aes(Var, Val)) +
geom_jitter(width = 0.2) +
stat_summary(geom = "crossbar",
fun.y = mean,
fun.ymax = mean,
fun.ymin = mean,
color = "red",
width = 0.4)
如何使y轴值缩放?使中立型上升到0.891,情感型上升到2.72?您可以使用ylim()
。但我认为轴标度应该反映总结值,而不是单个点。当然,如果使用箱线图或抖动,这不是问题。如何使y轴值缩放?使中立型上升到0.891,情感型上升到2.72?您可以使用ylim()
。但我认为轴标度应该反映总结值,而不是单个点。当然,如果使用箱线图或抖动,这不是问题。