R 用3个标签缩放5个刻度
我有5个条件:R 用3个标签缩放5个刻度,r,ggplot2,axis-labels,R,Ggplot2,Axis Labels,我有5个条件: labels = c("Baseline","Passenger Drive","Passenger Drive","Remote Drive","Remote Drive") 我想把“乘客驾驶”和“远程驾驶”这两个标签放在现有点的中间 玩具数据集: df <- data.frame(cbind(cbind(condition = c("Baseline","Passenger Drive", "Passenger D
labels = c("Baseline","Passenger Drive","Passenger Drive","Remote Drive","Remote Drive")
我想把“乘客驾驶”和“远程驾驶”这两个标签放在现有点的中间
玩具数据集:
df <- data.frame(cbind(cbind(condition = c("Baseline","Passenger Drive",
"Passenger Drive","Remote Drive","Remote Drive"),
rt_type = c("none",rep(c("driver_rt","other_rt"),2))),
rt = c(.4,.6,.5,.7,.62)))
ggplot(data = df,aes(x = interaction(rt_type,condition), y = rt)) +
theme_classic() +
geom_line(group = 1, size = 1) +
geom_point(size = 3) +
scale_x_discrete(labels = c("Baseline",
"Passenger Drive",
"Remote Drive")) +
labs(x = "Condition by Speaker", y = "Reaction Time (s)",
linetype = "Responder", shape = "Speaker")
df解决方案
简单地改变
scale_x_discrete(labels = c("Baseline",
"Passenger Drive",
"Remote Drive")) +
到
可取的
我知道你没有要求一个更有效的方法,但我认为一个变量(例如rt_类型)应该映射到点形状
ggplot(data = df, aes(x = condition, y = rt, shape = rt_type)) +
theme_classic() +
geom_point(size = 3,) +
scale_x_discrete(labels = c("Baseline",
"Passenger Drive",
"Remote Drive")) +
labs(
x = "Condition by Speaker",
y = "Reaction Time (s)"
)
您可以为x轴创建虚拟数字变量,并使用scale\u x\u continuous
而不是scale\u x\u discrete
# This replaces interaction(rt_type, condition)
df$intr <- as.numeric(as.factor(interaction(df$rt_type, df$condition)))
# Creating dummy mid point to place labels in the middle
ref_avg <- aggregate(intr ~ condition, df, mean)
df$my_breaks <- ref_avg[match(df$condition, ref_avg$condition), "intr"]
ggplot(data = df,aes(x = intr, y = rt)) +
theme_classic() +
geom_point(size = 3) +
geom_path(group = 1) +
scale_x_continuous(breaks = df$my_breaks, labels = df$condition) +
labs(x = "Condition by Speaker", y = "Reaction Time (s)",
linetype = "Responder", shape = "Speaker")
#这将替换交互(rt#U类型、条件)
df$intr谢谢!最后,它需要一个略为复杂的伪代码1 1 2 3 2 3 5 6 5 6(由于其他变量),但它可以使用scale_x_continuous。感谢您的建议。我提到了效率,因为线型和形状已经映射到其他变量,因为绘图比玩具数据集稍微复杂一些。
# This replaces interaction(rt_type, condition)
df$intr <- as.numeric(as.factor(interaction(df$rt_type, df$condition)))
# Creating dummy mid point to place labels in the middle
ref_avg <- aggregate(intr ~ condition, df, mean)
df$my_breaks <- ref_avg[match(df$condition, ref_avg$condition), "intr"]
ggplot(data = df,aes(x = intr, y = rt)) +
theme_classic() +
geom_point(size = 3) +
geom_path(group = 1) +
scale_x_continuous(breaks = df$my_breaks, labels = df$condition) +
labs(x = "Condition by Speaker", y = "Reaction Time (s)",
linetype = "Responder", shape = "Speaker")