首页 > 解决方案 > 使地铁图包括 ggplot2 r 中的 102 个主题

问题描述

这是ggplot2 中三个数据集的词频地铁样式图的后续内容

我在这个问题的答案中使用了代码,但正在努力解决如何最好地操作图表以使其适合地铁图中的 100 个唯一 dict 条目,而不会完全弄乱边缘上的 dict 词条目。

我测试了输入地铁图的不同数量的单词,发现它不能包含超过 25 个单词。

我有数据:

structure(list(dict = c("apple", "apple", "apple", 
"mandarin", "mandarin", "mandarin", "orange", "orange", "orange", "pear"), 
    name = c("freq_ongov", "freq_onindiv", "freq_onmedia", "freq_ongov", 
    "freq_onindiv", "freq_onmedia", "freq_ongov", "freq_onindiv", 
    "freq_onmedia", "freq_ongov"), value = c(0, 87, 63, 0, 44, 
    20, 3, 27, 25, 0), rank = c(26, 85, 70, 26, 61, 42.5, 86, 
    47, 48, 26)), row.names = c(NA, -10L), groups = structure(list(
    name = c("freq_ongov", "freq_onindiv", "freq_onmedia"), .rows = structure(list(
        c(1L, 4L, 7L, 10L), c(2L, 5L, 8L), c(3L, 6L, 9L)), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 3L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

但是我想在以下代码中包含此数据中的 100 行:


leftlabels <- df$dict[df$name == "freq_ongov"]
leftlabels <- leftlabels[order(df$rank[df$name == "freq_ongov"])]

rightlabels <- df$dict[df$name == "freq_onmedia"]
rightlabels <- rightlabels[order(df$rank[df$name == "freq_onmedia"])]

ggplot(df, aes(name, rank, color = dict, group = dict)) +
  geom_line(size = 4) +
  geom_point(shape = 21, fill = "white", size = 4) +
  scale_y_continuous(breaks = seq(max(df$rank)), labels = leftlabels,
                     sec.axis = sec_axis(~., breaks = seq(max(df$rank)), 
                                         labels = rightlabels)) +
  scale_x_discrete(expand = c(0.01, 0)) +
  guides(color = guide_none()) +
  coord_cartesian(clip = "off") +
  theme(axis.ticks.length.y = unit(0, "points"))

我尝试更改y.inty 轴的宽度和宽度以适合 100 个单词,但这只会使 y 轴更长,而不会更改 y 轴上每个单词标签之间的间距,因此所有单词都被挤在一起。有什么建议么?

标签: rggplot2tidyr

解决方案


推荐阅读