R - ggplot2:在 y 轴的另一侧设置我的条形图的值
R - ggplot2 : set values of my barchart on the other side of the y-axis
我想将“stop_out”变量的值设置在 y 轴的负侧,并在正侧保持“stop_in”的值。请问如何更正我的代码?
library(tidyr)
library(dplyr)
library(ggplot2)
tramway_df <- data.frame(
stop_order = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17),
stop_id = c(9943, 5041, 5361, 2902, 2903, 5872, 5802, 5803, 5804, 5805,
5710, 5711, 5712, 1643, 5714, 521, 529),
stop_name = c("DA VINCI", "DA VINCI", "BORDET STATION", "VAN CUTSEM", "FONSON", "PAIX",
"TILLEUL", "HELMET", "FOYER SCHAERBEEK.", "WAELHEM", "VERBOEKHOVEN", "PAVILLON",
"RUBENS", "LIEDTS", "THOMAS", "GARE DU NORD", "ROGIER"),
stop_in = c(27, 102, 153, 141, 190, 317, 379, 438, 328, 214, 278, 298, 154, 144, 162, 185, 0),
stop_out = c(0, 7, 3, 10, 22, 37, 81, 122, 143, 168, 291, 230, 149, 301, 412, 707, 821),
stop_charge = c(27, 121, 271, 402, 571, 851, 1149, 1465, 1650, 1696, 1682, 1745, 1750, 1592, 1342, 821, 0)
)
tramway_df %>%
gather(movement, value, stop_in:stop_out) %>%
mutate(stop_id = factor(stop_id, levels = unique(stop_id))) %>%
ggplot(aes(x=stop_id)) +
geom_col(aes(y=value, fill = movement), position = "dodge", width = 1) +
geom_line(aes(y=stop_charge, group=1)) +
scale_x_discrete(breaks = tramway_df$stop_id,
labels = tramway_df$stop_name)
这是你想要的吗?
library(tidyr)
library(dplyr)
library(ggplot2)
tramway_df %>%
gather(movement, value, stop_in:stop_out) %>%
mutate(stop_id = factor(stop_id, levels = unique(stop_id)),
value = ifelse(movement == "stop_out", -value, value))%>%
ggplot(aes(x=stop_id)) +
geom_col(aes(y=value, fill = movement), width = 0.9) +
geom_line(aes(y=stop_charge, group=1)) +
scale_x_discrete(breaks = tramway_df$stop_id,
labels = tramway_df$stop_name)+
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
由 reprex package (v2.0.0)
于 2021-09-12 创建
我想将“stop_out”变量的值设置在 y 轴的负侧,并在正侧保持“stop_in”的值。请问如何更正我的代码?
library(tidyr)
library(dplyr)
library(ggplot2)
tramway_df <- data.frame(
stop_order = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17),
stop_id = c(9943, 5041, 5361, 2902, 2903, 5872, 5802, 5803, 5804, 5805,
5710, 5711, 5712, 1643, 5714, 521, 529),
stop_name = c("DA VINCI", "DA VINCI", "BORDET STATION", "VAN CUTSEM", "FONSON", "PAIX",
"TILLEUL", "HELMET", "FOYER SCHAERBEEK.", "WAELHEM", "VERBOEKHOVEN", "PAVILLON",
"RUBENS", "LIEDTS", "THOMAS", "GARE DU NORD", "ROGIER"),
stop_in = c(27, 102, 153, 141, 190, 317, 379, 438, 328, 214, 278, 298, 154, 144, 162, 185, 0),
stop_out = c(0, 7, 3, 10, 22, 37, 81, 122, 143, 168, 291, 230, 149, 301, 412, 707, 821),
stop_charge = c(27, 121, 271, 402, 571, 851, 1149, 1465, 1650, 1696, 1682, 1745, 1750, 1592, 1342, 821, 0)
)
tramway_df %>%
gather(movement, value, stop_in:stop_out) %>%
mutate(stop_id = factor(stop_id, levels = unique(stop_id))) %>%
ggplot(aes(x=stop_id)) +
geom_col(aes(y=value, fill = movement), position = "dodge", width = 1) +
geom_line(aes(y=stop_charge, group=1)) +
scale_x_discrete(breaks = tramway_df$stop_id,
labels = tramway_df$stop_name)
这是你想要的吗?
library(tidyr)
library(dplyr)
library(ggplot2)
tramway_df %>%
gather(movement, value, stop_in:stop_out) %>%
mutate(stop_id = factor(stop_id, levels = unique(stop_id)),
value = ifelse(movement == "stop_out", -value, value))%>%
ggplot(aes(x=stop_id)) +
geom_col(aes(y=value, fill = movement), width = 0.9) +
geom_line(aes(y=stop_charge, group=1)) +
scale_x_discrete(breaks = tramway_df$stop_id,
labels = tramway_df$stop_name)+
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
由 reprex package (v2.0.0)
于 2021-09-12 创建