如何去除部分y轴?

问题描述 投票:0回答:1

我需要在同一张图上输出两张图表,一张以摄氏度显示年平均气温,另一张以华氏度显示。由于图表在 [ 15;40] 到 y 内是空的,我想删除图表的这一部分

'library(readr)
library(plotly)
library(tidyverse)
library(tidyquant)
library(ggplot2)
require(dplyr)

global_temp <- read_csv("X:/global_temp.csv")
View(global_temp)

global_temp$avg_temp_fatenheit <- (9/5 * global_temp['avg_temp'] + 32)
global_temp$avg_temp_fatenheit <- as.numeric(unlist(global_temp$avg_temp_fatenheit))

str(global_temp)

global_temp %>%
  mutate(bin = avg_temp < 15 & avg_temp_fatenheit > 40 ) %>%
  
ggplot() +
  facet_grid(bin ~ ., scale='free_y') +
  geom_line(global_temp, mapping = aes(x = year, y = avg_temp),color = '#6C22A6') +
  geom_point(global_temp, mapping = aes(x = year, y = avg_temp),color = '#6C22A6')+
  geom_line(global_temp, mapping = aes(x = year, y = avg_temp_fatenheit),color = '#96E9C6') +
  geom_point(global_temp, mapping = aes(x = year, y = avg_temp_fatenheit),color = '#96E9C6')+
  labs(title = 'Avarage temperature per year', x = 'Year', y='Temperature (°C - purple) (°F - green)', )+
  scale_x_continuous(limits = c(1750, 2015)) +
  scale_y_continuous(n.breaks = 10)+
  theme_bw()`

我尝试过设置限制,但它只适用于一张图

r ggplot2 plotly tidyverse visualization
1个回答
0
投票

提供数据片段是最佳实践,我认为这可能会对您有所帮助:

# step 1 provide an example of the global temperature data
read_csv("X:/global_temp.csv") %>% 
  mutate(avg_temp_farenheit = 9/5 * avg_temp + 32,
        bin = ifelse(avg_temp < 15 & avg_temp_farenheit > 40), 1, 0) %>%  
  ggplot(aes(year, avg_tmp)) +
  geom_line(color = '#6C22A6')) +
  geom_line(aes(y = avg_temp_farenheit), color = '#96E9C6') + 
  facet_wrap(~bin, scales = 'free_y') 
© www.soinside.com 2019 - 2024. All rights reserved.