gpt4 book ai didi

r - 使用因子变量 R 手动更改 y 轴刻度线标签

转载 作者:行者123 更新时间:2023-12-03 22:18:28 34 4
gpt4 key购买 nike

我有以下数据:

id <- rep(1:100)
A <- rep(c(0.12 ,0.25, 0.5, 1, 2), each = 20)
B <- rep(c(0.06, 0.03, 0.015, 0.12), each = 25)
C <- rep(c(0.015, 0.03, 0.06, 0.12, 0.25), each = 20)

df <- data.frame(id,A,B,C,stringsAsFactors = F)

我将 A、B 和 C 分成两列。请注意,A、B 和 C 列实际上是因子,我只是避免将它们指定为创建 fiddle 图的因子。
library(dplyr)

df_edited <- df %>%
gather(key, value, -id, factor_key = F)

我用这些数据创建了以下图:
library(ggplot2)

factor_breaks <- c(0.015,0.03,0.06,0.12,0.25,0.5,1,2)
factor_levels <- c("0.015","0.03","0.06","0.12","0.25","0.5","1","2")

ggplot(df_edited, aes(key, value))+
geom_violin()+
scale_y_continuous(labels = factor_levels, breaks = factor_breaks)

这将创建以下图:
enter image description here

是否可以使 y 轴标签安全地均匀放置,如下图所示,并且仍然确保 fiddle 图是正确的?
ggplot(df_edited, aes(key, factor(value)))+
geom_violin()

enter image description here

最佳答案

一种选择是转换 y 轴的值

ggplot(df_edited, aes(key, value))+
geom_violin()+
scale_y_continuous(labels = factor_levels,
breaks = factor_breaks,
trans = 'log10')

enter image description here

关于r - 使用因子变量 R 手动更改 y 轴刻度线标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48583894/

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