为什么在我尝试使用 r 中的 ylim 函数限制我的 y 轴时出现错误
why do I get an error when I try to limit my y-axis with ylim function in r
我无法将我的 y 极限值编辑到所需的比例。这是代码:
df <- read.table(text = "Site Type Day1 Day2 Day3
A SD 780 431 295
B SD 350 377 255
B M 480 179 560
A M 240 876 789
C FO 840 179 NA
C FI 350 NA 255
A NF 508 NA 565
B NF 405 876 NA ", header = TRUE)
library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
pivot_longer(cols = -c(Site,Type)) %>%
mutate(Type = factor(Type,
levels = c('SD', 'M', 'NF', 'FO', 'FI'),
ordered = T)) %>%
ggplot(aes(x=Site,y=value,fill=name, ylim=c(0,25000)))+
geom_bar(stat='identity')+
facet_wrap(.~Type, scales = 'free_x', ncol = 5, strip.position = "bottom")+
theme_minimal()+
theme(strip.placement = "outside",
panel.spacing = unit(0, "points"),
strip.background = element_blank(),
strip.background.y = element_blank(),
panel.background = element_rect(fill = "yellow"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
strip.text = element_text(face = "bold", size = 9))+
scale_color_discrete(name="Legend",label=c("Day 1","Day 2", "Day 3"))
但是当我删除 ylim=c(0,25000)
它不会带来错误。我想将其应用于另一个具有非统一比例的类似数据集。
您需要使用 ggplot2
中的 ylim()
函数:
df %>%
pivot_longer(cols = -c(Site,Type)) %>%
mutate(Type = factor(Type,
levels = c('SD', 'M', 'NF', 'FO', 'FI'),
ordered = T)) %>%
ggplot(aes(x=Site,y=value,fill=name))+
ylim(0,25000) +
geom_bar(stat='identity')+
facet_wrap(.~Type, scales = 'free_x', ncol = 5, strip.position = "bottom")+
theme_minimal()+
theme(strip.placement = "outside",
panel.spacing = unit(0, "points"),
strip.background = element_blank(),
strip.background.y = element_blank(),
panel.background = element_rect(fill = "yellow"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
strip.text = element_text(face = "bold", size = 9))+
scale_color_discrete(name="Legend",label=c("Day 1","Day 2", "Day 3"))
我无法将我的 y 极限值编辑到所需的比例。这是代码:
df <- read.table(text = "Site Type Day1 Day2 Day3
A SD 780 431 295
B SD 350 377 255
B M 480 179 560
A M 240 876 789
C FO 840 179 NA
C FI 350 NA 255
A NF 508 NA 565
B NF 405 876 NA ", header = TRUE)
library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
pivot_longer(cols = -c(Site,Type)) %>%
mutate(Type = factor(Type,
levels = c('SD', 'M', 'NF', 'FO', 'FI'),
ordered = T)) %>%
ggplot(aes(x=Site,y=value,fill=name, ylim=c(0,25000)))+
geom_bar(stat='identity')+
facet_wrap(.~Type, scales = 'free_x', ncol = 5, strip.position = "bottom")+
theme_minimal()+
theme(strip.placement = "outside",
panel.spacing = unit(0, "points"),
strip.background = element_blank(),
strip.background.y = element_blank(),
panel.background = element_rect(fill = "yellow"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
strip.text = element_text(face = "bold", size = 9))+
scale_color_discrete(name="Legend",label=c("Day 1","Day 2", "Day 3"))
但是当我删除 ylim=c(0,25000)
它不会带来错误。我想将其应用于另一个具有非统一比例的类似数据集。
您需要使用 ggplot2
中的 ylim()
函数:
df %>%
pivot_longer(cols = -c(Site,Type)) %>%
mutate(Type = factor(Type,
levels = c('SD', 'M', 'NF', 'FO', 'FI'),
ordered = T)) %>%
ggplot(aes(x=Site,y=value,fill=name))+
ylim(0,25000) +
geom_bar(stat='identity')+
facet_wrap(.~Type, scales = 'free_x', ncol = 5, strip.position = "bottom")+
theme_minimal()+
theme(strip.placement = "outside",
panel.spacing = unit(0, "points"),
strip.background = element_blank(),
strip.background.y = element_blank(),
panel.background = element_rect(fill = "yellow"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
strip.text = element_text(face = "bold", size = 9))+
scale_color_discrete(name="Legend",label=c("Day 1","Day 2", "Day 3"))