r-如何在ggplot2条形图中使正值和负值朝同一方向


group = letters[1:10]
response = sample(-5:5, size=10, replace=TRUE)
df <- data.frame(group,
response)

我想把这个数字做成这个数字,希望结果能显示我研究的价值,但我的数据既有负值,也有正值。我想知道如何改变y轴,使它们朝同一方向。我试图通过scale_y_continuous()设置y轴限制,但没有成功。查看我的结果。非常感谢您提供任何可能的解决方案。

这是我的绘图代码

ggplot(df) +
aes(x = group, y = response, fill = response) +
geom_col() +
scale_fill_distiller(palette = "PiYG", 
direction = 1) +
theme_minimal() +
theme(legend.position = "bottom")+coord_polar()

怎么样,而不是"响应";,将腹肌(反应(映射到你的y轴?我假设你希望te颜色仍然反映原始值,所以你可以保持填充映射到";响应";。

像这样:

ggplot(df) +
aes(x = group, y = abs(response), fill = response) +
geom_col() +
scale_fill_distiller(palette = "PiYG", 
direction = 1) +
theme_minimal() +
theme(legend.position = "bottom")+coord_polar()

您可以尝试使用abs(response)生成y轴的绝对值。根据您共享的理想输出,您还可以尝试使用reorder(strwrap(x,10),y)对组进行重新排序,以使组从响应值最小的组排序为最大的组。此外,我认为theme_gray()theme(legend.position = "right")很好地完成了绘图。我还添加了以下行来正确标记您的轴:

Response = abs(response)#y-axis
Group = reorder(strwrap(group, 10), 
Response)#x-axis

顺便说一句,我是新来的,所以如果我的文章和代码没有很好地嵌入,请原谅我:(

这是我的代码:

library(ggplot2)
group = letters[1:10]
response = sample(-5:5, size=10, replace=TRUE)
df <- data.frame(group, response)
Response = abs(response)
Group = reorder(strwrap(group, 10), 
Response)
ggplot(df) +
aes(Group, Response, fill = response) +
geom_col()+
theme_gray()+
scale_fill_distiller(palette = "PiYG", 
direction = 1) +
theme(legend.position = "right")+
coord_polar()

最新更新