所以基本上我已經創建了垃圾箱并擁有每個垃圾箱的方法,將這兩列放在一個資料框中。現在我正在繪制這兩列,但我想要確切的數字作為 x lable 而不是 bin。我正在考慮將每個 bin 重命名為中點。請看圖片。第一個是我當前的情節,第二個是我想要實作的情節。
我目前的情節:
我想要的:
我的資料框是這樣的:

uj5u.com熱心網友回復:
要重現您包含的繪圖影像的樣式,您可以執行以下操作:
library(tidyverse)
df %>%
mutate(bin_group = gsub("\\(|\\]", "", bin_group)) %>%
separate(bin_group, sep = ",", into = c("lower", "upper")) %>%
mutate(across(lower:upper, as.numeric)) %>%
mutate(`Birth weight (g)` = (upper lower) / 2) %>%
ggplot(aes(`Birth weight (g)`, mean_28_day_mortality))
geom_vline(xintercept = 1500)
geom_point(shape = 18, size = 4)
scale_x_continuous(labels = scales::comma)
labs(title = "One-year mortality", y = NULL)
theme_bw(base_family = "serif", base_size = 20)
theme(panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_line(color = "black", size = 0.5),
plot.title = element_text(hjust = 0.5))

編輯
要對范圍進行特定更改,請使用limits引數 in scale_x_continuous,scale_y_continuous您可以執行以下操作:
library(tidyverse)
df %>%
mutate(bin_group = gsub("\\(|\\]", "", bin_group)) %>%
separate(bin_group, sep = ",", into = c("lower", "upper")) %>%
mutate(across(lower:upper, as.numeric)) %>%
mutate(`Birth weight (g)` = (upper lower) / 2) %>%
ggplot(aes(`Birth weight (g)`, mean_28_day_mortality))
geom_vline(xintercept = 1500)
geom_point(shape = 18, size = 4)
scale_x_continuous(labels = scales::comma, limits = c(1350, 1650),
breaks = seq(1350, 1650, 50))
scale_y_continuous(limits = c(0, 0.1), name = NULL)
labs(title = "One-year mortality")
theme_bw(base_family = "serif", base_size = 20)
theme(panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_line(color = "black", size = 0.5),
plot.title = element_text(hjust = 0.5))
使用的資料(使用 OCR 從相關影像中獲得)
df <- structure(list(bin_group = structure(1:10,
levels = c("(1.35e 03,1.38e 03]",
"(1.38e 03,1.41e 03]", "(1.41e 03,1.44e 03]", "(1.44e 03,1.47e 03]",
"(1.47e 03,1.5e 03]", "(1.5e 03,1.53e 03]", "(1.53e 03,1.56e 03]",
"(1.56e 03,1.59e 03]", "(1.59e 03,1.62e 03]", "(1.62e 03,1.65e 03]"
), class = "factor"), mean_28_day_mortality = c(0.0563498, 0.04886257,
0.04467626, 0.04256053, 0.04248667, 0.04009187, 0.03625538, 0.03455094,
0.03349542, 0.02892909)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L))
uj5u.com熱心網友回復:
如果您有(我假設)您使用 制作的組cut,您可以提取最大值和最小值,然后在匯總和繪圖之前計算平均值。請注意,我將正則運算式寫得很長,因為我個人不知道 cut 是否總是使左或包含或排他。
library(tidyverse)
#example like yours
mtcars |>
mutate(grp = cut(hp, 10)) |>
group_by(grp) |>
summarise(mpg_mean = mean(mpg)) |>
ggplot(aes(grp, mpg_mean))
geom_point()

#solution
mtcars |>
mutate(grp = cut(hp, 10)) |>
extract(grp,
into = c("min", "max"),
remove = FALSE,
regex = "(?:\\(|\\[)(.*),(.*)(?:\\)|\\])",
convert = TRUE) |>
mutate(mean_grp = (min max)/2)|>
group_by(mean_grp) |>
summarise(mpg_mean = mean(mpg)) |>
ggplot(aes(mean_grp, mpg_mean))
geom_point()

編輯
如果您只想重新標記而不實際轉換資料,這是另一種選擇:
lab_fun <- function(x){
str_split(x, ",") |>
map_dbl(~parse_number(.x)
|> mean())
}
mtcars |>
mutate(grp = cut(hp, 10)) |>
group_by(grp) |>
summarise(mpg_mean = mean(mpg)) |>
ggplot(aes(grp, mpg_mean))
geom_point()
scale_x_discrete(labels = lab_fun)

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/529690.html
標籤:rggplot2垃圾箱

