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

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

#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)

给TA打赏
共{{data.count}}人
人已打赏
解决方案

用Spark读取庞大的CSV文件

2022-4-21 9:00:14

解决方案

在Visual Studio中使用C#有办法让一个面板缓慢移动吗?

2022-4-21 10:09:01

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索