在下图中:
Y轴的值为1.9999999925 (对于99个步骤)和1.9999999882 (对于其余步骤)。正如您所看到的,绘图只显示值2,因为它将两个值的小数四舍五入为2。如何避免这种情况,并按原样显示两个值?
这是我用来生成绘图的R脚本:
dataset <- readr::read_csv("/data.csv")
dataset <- dataset %>% melt(id.vars = c("Class"))
dataset <- transform(dataset, value = value)
YaxisTitle <- "Fitness"
class <- "Steamer"
p2_data <- dataset %>% filter(Class == class)
pp2 <- p2_data %>% ggplot(aes(x=factor(variable), y=value, group=Class, colour=Class)) + geom_line() + scale_x_discrete(breaks = seq(0, 1000, 100)) + labs(x = "Steps", y = YaxisTitle) + theme(legend.position="none")
发布于 2019-08-08 20:38:01
如果我在How do I change the number of decimal places on axis labels in ggplot2?中根据接受的答案调整解决方案,我确实会设法更改标签。
关键是将scale函数scaleFUN
更改为允许超过原始post中的2个小数到9个小数(或您需要的任何数字)。所以从"%.2f"
到"%.9f"
。
df <- data.frame(Steps=1:1000, Fitness=c(rep(1.9999999925,99),rep(1.9999999882,901)))
library(ggplot2)
scaleFUN <- function(x) sprintf("%.9f", x)
ggplot(data=df,aes(x=Steps,y=Fitness)) +
geom_line() +
scale_y_continuous(labels=scaleFUN)
https://stackoverflow.com/questions/57412266
复制相似问题