英文:
Overlapping Boxplot and Cloudplot with two Y-Axes in ggplot
问题
你的代码试图绘制一个箱线图和云图,其中第一个Y轴是一个字符变量(1,0),第二个Y轴是一个介于(0,1)之间的数值变量。你想要使两个数据点/箱线图重叠在同一比例上,并且希望有2个Y轴而不是图例。
以下是你的代码的翻译部分:
ggplot(df, aes(x = VarContinuaX)) +
geom_boxplot(aes(y = VarBinariaY), outlier.shape = NA) +
coord_cartesian(xlim = c(-1, 1)) +
geom_point(aes(y = VarContinuaY, color = as.factor(VarBinariaY))) +
scale_color_manual(values = c("blue", "red"),
labels = c("0", "1"),
guide = guide_legend(title = "Recommend")) +
labs(y = "第一个轴", color = "第二个轴")
这应该是你代码的翻译部分,不包括问题的回答。
英文:
I have a dataframe like df:
df <- structure(list(VarBinariaY = c("1", "1", "0", "1", "0", "1",
"1", "1", "1", "1"), VarContinuaX = c(0.42125112307258, 0.403089860687032,
0.501541076926515, 0.285735354525968, 0.838761311257258, 0.728340754751116,
0.547309613088146, 0.758638551225886, 0.664854867151007, 0.730170585680753
), VarContinuaY = c(0.62294910964556, 0.153794215992093, 0.173481315141544,
0.441902326885611, 0.556428883690387, 0.300496574025601, 0.778148024342954,
0.358530796831474, 0.163795455591753, 0.658571016974747)), row.names = c(NA,
10L), class = "data.frame")
and I want to plot a boxplot and a cloudplot having in the first Y-axis a character variable (1, 0), and in the second Y-axis a numeric variable between (0,1). I want both data points/boxplot to overlap with each other, so to be in the same scale. However, geom_point stays always below the scale of the first Y-axis. Moreover, I would like to have 2 Y-axis instead of a legend.
Here is my trial:
ggplot(df, aes(x = VarContinuaX)) +
geom_boxplot(aes(y = VarBinariaY ), outlier.shape = NA) +
coord_cartesian(xlim = c(-1, 1)) +
geom_point(aes(y = VarContinuaY, color = as.factor(VarBinariaY ))) +
scale_color_manual(values = c("blue", "red"),
labels = c("0", "1"),
guide = guide_legend(title = "Recommend")) +
labs(y = "First Axis", color = "Second Axis")[![enter image description here][1]][1]
Any clue?
答案1
得分: 1
以下是您要的翻译:
由于您的数据点位于连续的Y轴刻度上,实现这一目标的最简单方法是将箱线图放在相同的Y轴刻度上。要实现这一点,您只需将字符变量转换为数字,因为它的级别已经是0和1。如果将其保留为字符变量,ggplot会首先将其转换为因子,然后使用因子级别的底层整数表示作为Y轴位置。然而,整数始终从1开始,因此Y轴位置将是1和2,而不是所期望的0和1。
ggplot(df, aes(x = VarContinuaX)) +
geom_boxplot(aes(y = as.numeric(VarBinariaY), group = VarBinariaY),
outlier.shape = NA) +
geom_point(aes(y = VarContinuaY, color = VarBinariaY)) +
scale_y_continuous("第一轴", breaks = 0:1,
sec.axis = sec_axis(~.x, "第二轴")) +
scale_color_manual(values = c("蓝色", "红色"), guide = "none") +
coord_cartesian(xlim = c(0, 1)) +
theme_gray(base_size = 16)
英文:
Since your points are on a continuous Y scale, the easiest way to achieve this is to have your boxplots on the same Y scale. To achieve this, you need only convert the character variable to numeric, since its levels are already 0 and 1. If you leave it as a character variable, ggplot will first convert it to a factor, then use the underlying integer representation of the factor levels as the Y axis positions. However, the integers always start at 1, so the Y axis positions will be 1 and 2, not 0 and 1 as desired.
ggplot(df, aes(x = VarContinuaX)) +
geom_boxplot(aes(y = as.numeric(VarBinariaY), group = VarBinariaY),
outlier.shape = NA) +
geom_point(aes(y = VarContinuaY, color = VarBinariaY)) +
scale_y_continuous("First axis", breaks = 0:1,
sec.axis = sec_axis(~.x, "Second axis")) +
scale_color_manual(values = c("blue", "red"), guide = "none") +
coord_cartesian(xlim = c(0, 1)) +
theme_gray(base_size = 16)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论