将图形改为对数比例,但轴保持在非对数比例中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将图形改为对数比例,但轴保持在非对数比例中相关的知识,希望对你有一定的参考价值。

我现在的代码是这样的(来自于网站上很多人的帮助,所以谢谢你们!)。

#my data
DF <- data.table("Site_No" = c(123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123), 
 "variable" = 
c(1,1,2,2,5,5,10,10,20,20,25,25,30,30,40,40,50,50,60,60,70,70,75,75,80,80,90,90,95,95,98,98,99,99), 
"Test" = c("value.x", "value.y", "value.x", "value.y", "value.x", "value.y","value.x", "value.y", 
"value.x", "value.y", "value.x", "value.y","value.x", "value.y", "value.x", "value.y", "value.x", 
"value.y","value.x", "value.y", "value.x", "value.y", "value.x", "value.y","value.x", "value.y", 
"value.x", "value.y","value.x", "value.y","value.x", "value.y","value.x", "value.y"),                                                                                                                                               
"Value" = c(800, 700, 600, 580, 570, 560, 520, 500, 430, 415, 412, 402, 380, 370, 366, 300, 290, 280, 
270, 260,  250, 240, 230, 220, 210, 200, 180, 160, 150, 130, 100, 95, 67, 40))


#create plots
for (i in unique(DF$Site_No)){
graph <- ggplot(data = DF[Site_No == i,], mapping = aes(x = variable, y = Value, group 
                                                      = Test)) +
geom_line(aes(color = Test)) +
scale_color_discrete(labels = c("annual","winter"))+
scale_x_discrete(name ="Percent Chance Flow Exceeded", 
                     limits=c("1", "10", "20", "30", "40", "50", "60", "70", "80", "90", "99"))+
labs(x = "Percent Chance Flow Exceeded", y = "Streamflow, In cubic feet per second")+
ggtitle(paste(i))

ggsave(filename = paste0("Site_",i,".png"), plot = graph, device = "png", width  = 5, height = 5, units = "in")
}

以下是我的要求,请您修改!1)将x轴的间距改为:0.5、2、5、10、20、30、40、60、70、80、90、95、98、99。

2) 将两条线(年线和冬线)都做成对数比例,但Y轴不要做成对数比例!

非常感谢你们的帮助:)!!

答案

我使用的是data.frame,所以我可以使用常规的tidyverse管道。你需要在绘制之前转换你的数据。我会在把你的数据传给for循环之前这样做。我在这个例子中删除了这个循环,因为你的数据只包含一个ID。

为了在整数 "可读 "的数字上显示轴的中断,你需要这些整数的对数值作为轴的中断,然后对标签使用对数的指数......如果这有意义的话。请看代码。

library(tidyverse)
DF <- data.frame("Site_No" = c(123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123), 
                 "variable" = 
                   c(1,1,2,2,5,5,10,10,20,20,25,25,30,30,40,40,50,50,60,60,70,70,75,75,80,80,90,90,95,95,98,98,99,99), 
                 "Test" = c("value.x", "value.y", "value.x", "value.y", "value.x", "value.y","value.x", "value.y", 
                            "value.x", "value.y", "value.x", "value.y","value.x", "value.y", "value.x", "value.y", "value.x", 
                            "value.y","value.x", "value.y", "value.x", "value.y", "value.x", "value.y","value.x", "value.y", 
                            "value.x", "value.y","value.x", "value.y","value.x", "value.y","value.x", "value.y"),                                                                                                                                               
                 "Value" = c(800, 700, 600, 580, 570, 560, 520, 500, 430, 415, 412, 402, 380, 370, 366, 300, 290, 280, 
                             270, 260,  250, 240, 230, 220, 210, 200, 180, 160, 150, 130, 100, 95, 67, 40))


DF %>% mutate(logval = log(Value)) %>%
  ggplot(mapping = aes(x = variable, y = logval, group  = Test)) +
  geom_line(aes(color = Test)) +
  scale_color_discrete(labels = c("annual","winter")) +
  scale_y_continuous(breaks = log(seq(100,800,100)), labels = exp(log(seq(100,800,100)))) +
  scale_x_discrete(name ="Percent Chance Flow Exceeded", 
                   limits=c( 0.5, 2, 5, 10, 20, 30, 40, 60, 70, 80, 90, 95, 98, 99)) +
  labs(x = "Percent Chance Flow Exceeded", y = "Streamflow, In cubic feet per second")

创建于 2020-04-22 由 重读包 (v0.3.0)

以上是关于将图形改为对数比例,但轴保持在非对数比例中的主要内容,如果未能解决你的问题,请参考以下文章

R ggplot:使用对数比例的错误geom_arrow方向

如何解释R中原始比例混合模型的对数变换模型的系数?

对数刻度返回 NaN

用 ggplot2 注释 R 中的矩形以获得对数刻度的图形

幂函数 指数 对数 底数

R语言ggplot2可视化:ggplot2可视化密度图(显示数据密集区域)ggplot2可视化密度图(对数坐标):log10比例的收入密度图突出了在常规密度图中很难看到的收入分布细节