gpt4 book ai didi

r - ggplot2 中辅助 y 轴的更好转换

转载 作者:行者123 更新时间:2023-12-03 23:43:31 28 4
gpt4 key购买 nike

我正在尝试为 ggplot2 的某些数据添加辅助 y 轴。我的资料 p看起来像这样:

  aspect  yhat Count
<dbl> <dbl> <dbl>
1 1.37 0.329 144
2 16.9 0.329 5
3 32.3 0.330 5
4 47.8 0.331 0
5 63.3 0.333 57
6 78.8 0.333 67
7 94.3 0.332 13
8 110. 0.332 0
9 125. 0.332 0
10 141. 0.331 0
我试图像这样绘制它:
 #get the information to for using a secondary y axis
ylim.prim <- c(min(p$yhat), max(p$yhat))
ylim.sec <- c(min(p$Count, na.rm = T), max(p$Count, na.rm = T))

b <- diff(ylim.prim)/diff(ylim.sec)
a <- b*(ylim.prim[1] - ylim.sec[1])

#now make plot
p1 = ggplot(p, aes(x = get(col), y = yhat)) +
geom_line() +
labs(x = col) +
geom_line(aes(y = a + Count*b), color = "red", linetype = 2) +
scale_y_continuous(name = "Mean Response", sec.axis = sec_axis(~ (. - a)/b, name = "Number of Pixels")) +
theme_light() +
theme(axis.line.y.right = element_line(color = "red"),
axis.ticks.y.right = element_line(color = "red"),
axis.text.y.right = element_text(color = "red"),
axis.title.y.right = element_text(color = "red")
) +
theme(legend.title = element_blank()) +
theme(text=element_text(size=22))
返回此图:
enter image description here
我遇到的问题是黑色和红色线条之间有太多未使用的空白空间,这使得解释这些值有点困难,我想知道是否可以使用更好的转换来使线条靠得更近,甚至重叠,但我不知道怎么做。
我认为这可能与相同 yhat 的事实有关。值可以有不同 count值。如果是这样,有没有办法解决这个问题?

最佳答案

总的来说,我同意多个(不同的)轴很容易让观众感到困惑的前提。使用颜色和不同的对象(点与线)有助于区分这两个值,但这是一种缓解措施。
这是一个刺,使用点(如建议):

count_slope_intercept <- c(m = diff(range(p$Count)), b = min(p$Count))
yhat_slope_intercept <- c(m = diff(range(p$yhat)), b = min(p$yhat))

p$Count2 <- yhat_slope_intercept["b"] +
yhat_slope_intercept["m"] * (p$Count - count_slope_intercept["b"]) / count_slope_intercept["m"]

ggplot(p, aes(x = aspect, y = yhat)) +
geom_line() +
labs(x = "aspect") +
geom_point(aes(y = Count2), color = "red") +
scale_y_continuous(
name = "Mean Response",
sec.axis = sec_axis(~ count_slope_intercept["b"] + count_slope_intercept["m"] *
(. - yhat_slope_intercept["b"]) / yhat_slope_intercept["m"])
) +
theme_light() +
theme(axis.line.y.right = element_line(color = "red"),
axis.ticks.y.right = element_line(color = "red"),
axis.text.y.right = element_text(color = "red"),
axis.title.y.right = element_text(color = "red")
) +
theme(legend.title = element_blank()) +
theme(text=element_text(size=22))
ggplot2 with two y-axes
(使用 scales 或类似方法可能有更顺畅的方法。)

编辑 : 是的,和 scales 一样:
p$Count3 <- scales::rescale(p$Count, to = range(p$yhat), from = range(p$Count))

ggplot(p, aes(x = aspect, y = yhat)) +
geom_line() +
labs(x = "aspect") +
geom_point(aes(y = Count3), color = "red") +
scale_y_continuous(
name = "Mean Response",
sec.axis = sec_axis(~ scales::rescale(., to = range(p$Count), from = range(p$yhat)))
) +
theme_light() +
theme(axis.line.y.right = element_line(color = "red"),
axis.ticks.y.right = element_line(color = "red"),
axis.text.y.right = element_text(color = "red"),
axis.title.y.right = element_text(color = "red")
) +
theme(legend.title = element_blank()) +
theme(text=element_text(size=22))
(虽然我在上面明确指出并包括 to=from= 参数, from 默认为 range(x) ,所以代码可以稍微缩短一些。我认为这个例子最好是明确的和显示即将发生的事情 from 在哪里和 to 在哪里。)

关于r - ggplot2 中辅助 y 轴的更好转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64324221/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com