R语言 棒棒糖图表数据可视化ggplot2

rqenqsqc  于 2023-01-15  发布在  其他
关注(0)|答案(2)|浏览(177)

我是Rstudio的新手,我想用一个水平的棒棒糖图来可视化与我的变量"UNEMP"相关的国家级数据,但我真的不知道该怎么准备我的数据(因为我的变量找不到),也不知道该用哪种代码来绘制它们。
以下是我的数据:

dput(data)

structure(list(COUNTRY = c("Austria", "Belgium", "Bulgaria", "Croatia", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "Netherlands", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden"), PERFAKE = c(31, 31, 40, 47, 45, 40, 25, 23, 18, 39, 24, 55, 52, 38, 37, 35, 23, 38, 27, 25, 48, 32, 41, 45, 31, 53, 25), FORDISINFO = c("2764", "3803", "1929", "3148", "2783", "2296", "3449", "1678", "3443", "3072", "1908", "3246", "1482", "2766", "3031", "0.382", "3.26", "2637", "3.4", "2867", "3317", "3484", "2768", "2744", "3457", "3322", "2896"), PARDISINFO = c("2322", "2778", "1523", "2.87", "3009", "2876", "3651", "2866", "2.84", "3235", "2926", "2244", "1113", "3621", "1951", "2893", "3465", "2916", "1208", "2859", "2656", "3533", "2485", "2519", "1382", "2971", "3345"), MEDIALIT = c(59, 66, 32, 43, 43, 51, 72, 70, 78, 59, 64, 40, 41, 69, 51, 54, 52, 62, 44, 71, 53, 62, 36, 44, 59, 57, 71), POLAR = c("1838", "1627", "0.68", "0.995", "2062", "1885", "3037", "1343", "3155", "0.613", "1521", "0.45", "0.023", "1664", "0.651", "1817", "2582", "3.25", "0.528", "1477", "0.048", "1695", "1344", "1004", "0.388", "0.12", "2136"), CULBACK = c(37, 36, 52, 39, 50, 49, 24, 37, 22, 38, 35, 63, 63, 25, 51, 41, 28, 17, 63, 34, 37, 26, 33, 54, 38, 26, 19), UNEMP = c(4.8, 5.9, 5.2, 8.4, 8.4, 2.2, 5.1, 5.4, 7.4, 9, 3.4, 19.3, 3.7, 5.7, 10.6, 7.4, 6.2, 5.6, 3.7, 3.8, 3.8, 7, 4.2, 6.5, 5.1, 15.3, 6.4), PERCOR = c(77, 75, 43, 47, 58, 44, 87, 74, 86, 69, 80, 48, 44, 74, 53, 56, 60, 80, 54, 82, 58, 62, 44, 50, 60, 62, 85), NOREPRES = c(40, 44, 44, 34, 64, 67, 14, 76, 33, 44, 24, 83, 50, 0, 72, 66, 53, 35, 40, 20, 36, 60, 58, 49, 50, 58, 12)), row.names = c(NA, -27L), class = "data.frame")

下面是我的变量被RStudio视为"缺失"的证据。

exists("UNEMP")
[1] FALSE

怎么办?先谢谢你。

rqmkfv5c

rqmkfv5c1#

下面是一个使用ggplot2实现预期结果的基本示例。基本上,您可以通过将geom_segmentgeom_point组合来创建一个棒棒糖图。此外,我还使用reorder按失业率(?)对国家进行排序:

library(ggplot2)

ggplot(data, aes(UNEMP, reorder(COUNTRY, UNEMP))) +
  geom_segment(aes(xend = 0, yend = COUNTRY)) +
  geom_point() +
  labs(y = NULL, x = "Unemployment rate")

t9aqgxwy

t9aqgxwy2#

另一个选项是使用lollipop_chartggcharts,它会自动排序:

library(ggcharts)
lollipop_chart(data, COUNTRY, UNEMP) +
  labs(y = "Unemployment rate", x = NULL)

相关问题