[英]Utilising multiple columns to be the x-axis in ggplot2 for R
我想獲得上面的圖表,我嘗試使用 ggplot 來獲取它。
blue = c(83, 81, 80, 71, 69, 63, 63, 62, 54)
red = c(112, 96, 111, 141, 125, 89, 178, 107, 130)
loc = c("YISHUN", "WOODLANDS", "HOUGANG", "ANG MO KIO" "TAMPINES", "SENGKANG", "GEYLANG", "BEDOK", "JURONG WEST")
tib = mutate(blue, red, loc)
ggplot(lst_compare, aes(x=c(wet, dry) ,y=loc)) +
geom_point() + geom_segment(y=loc, x= wet, xend= dry, yend= loc)
我嘗試了上述方法,但是,似乎不能簡單地將多列包含為 x 值。 有誰知道如何將多列作為 x 軸的一部分?
我修正了你的 reprex 中的錯誤。 我認為最好在繪圖之前對數據進行透視。
library(tidyverse)
blue = c(83, 81, 80, 71, 69, 63, 63, 62, 54)
red = c(112, 96, 111, 141, 125, 89, 178, 107, 130)
loc = c("YISHUN", "WOODLANDS", "HOUGANG", "ANG MO KIO", "TAMPINES", "SENGKANG", "GEYLANG", "BEDOK", "JURONG WEST")
tib = data.frame(blue, red, loc)
tib %>%
pivot_longer(c("blue", "red")) %>%
ggplot() +
geom_point(aes(x = value, y = loc, color = name), size = 2) +
scale_color_identity() +
geom_line(aes(x = value, y = loc)) +
theme_minimal()
由reprex 包(v2.0.1) 於 2021 年 10 月 21 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.