【问题标题】:Adjust the polar coordinates for geom_text with coord_polar使用 coord_polar 调整 geom_text 的极坐标
【发布时间】:2023-01-30 04:45:37
【问题描述】:

我正在尝试将一系列标签绘制成一个圆圈。我大部分都在那里,但它们压得太紧了,我想增加半径,以便它们可以散开一点。无论我为“y”变量设置什么值,它们都会继续聚集在一起,如下所示。

这是我尝试过的:

p <- ggplot() + 
    geom_text(data        =  hcdata,
              aes(x       =  x,
                  y       =  y+30,
                  label   =  label,
                  colour  =  factor(clust),
                  angle   =  angle),
              vjust       =  labelParams$vjust,
              hjust       =  labelParams$hjust,
              size        =  label.size,
              show.legend =  FALSE)

## orientation
p <- p +
      coord_polar(direction = 1) + 
      scale_x_continuous(breaks = NULL,
                         limits = c(0, nrow(hcdata))) +
      scale_y_reverse(breaks = ybreaks)

p

数据预览:

> head(hcdata)
  x y            label clust    angle
1 1 0              UCF     7 447.1277
2 2 0    Virginia Tech     7 443.2979
3 3 0          Hofstra     7 439.4681
4 4 0 SUNY Stony Brook     7 435.6383
5 5 0          U Miami     7 431.8085
6 6 0 Eastern Virginia     6 427.9787
 
> str(labelParams)
List of 3
 $ angle: num [1:94] 447 443 439 436 432 ...
 $ hjust: num [1:94] 0 0 0 0 0 0 0 0 0 0 ...
 $ vjust: num 0.5

hcdata 的数据在这里:

structure(list(x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 
62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 
94), y = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), label = c("UCF", "Virginia Tech", 
"Hofstra", "SUNY Stony Brook", "U Miami", "Eastern Virginia", 
"Temple", "Drexel", "Saint Louis", "NYMC", "Tufts", "Augusta", 
"Rutgers (RWJ)", "U Missouri", "U S Florida", "SUNY Buffalo", 
"U Kentucky", "West Virginia", "U Illinois", "U Missouri (Kansas City)", 
"Wayne State", "U Arizona", "SUNY Upstate", "Texas A&M", "Rutgers (Newark)", 
"UT Houston", "U Louisville", "Northwestern", "Columbia", "Vanderbilt", 
"Mount Sinai", "NYU", "Case Western", "Yale", "U Penn", "Johns Hopkins", 
"Harvard", "Duke", "WashU", "U Florida", "Stanford", "UVA", "Jefferson", 
"Rush", "U Rochester", "Baylor", "MUSC", "U Connecticut", "U Utah", 
"Georgetown", "U Chicago", "U Michigan", "Wake Forest", "Cornell", 
"Virginia Commonwealth", "U Pitt", "UC Irvine", "U Cincinnati", 
"Emory", "UCSD", "Ohio State", "George Washington", "UCLA", "UTSW", 
"Mayo", "Einstein", "USC (Keck)", "U Mass", "U Kansas", "U SC", 
"UTHSC", "U Arkansas", "Texas Tech", "U Oklahoma", "U New Mexico", 
"U Nebraska", "U Hawaii", "Dartmouth", "U Iowa", "U Wisconsin", 
"UT San Antonio", "UC Davis", "U Maryland", "U Vermont", "U Alabama", 
"Boston", "IndianaU", "Brown", "UNC Chapel Hill", "U Colorado", 
"U Washington", "OHSU", "U Minnesota", "UCSF"), clust = c(7, 
7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 1, 1, 1, 1, 1, 1), angle = c(447.127659574468, 443.297872340426, 
439.468085106383, 435.63829787234, 431.808510638298, 427.978723404255, 
424.148936170213, 420.31914893617, 416.489361702128, 412.659574468085, 
408.829787234043, 405, 401.170212765958, 397.340425531915, 393.510638297872, 
389.68085106383, 385.851063829787, 382.021276595745, 378.191489361702, 
374.36170212766, 370.531914893617, 366.702127659575, 362.872340425532, 
359.042553191489, 355.212765957447, 351.382978723404, 347.553191489362, 
343.723404255319, 339.893617021277, 336.063829787234, 332.234042553192, 
328.404255319149, 324.574468085106, 320.744680851064, 316.914893617021, 
313.085106382979, 309.255319148936, 305.425531914894, 301.595744680851, 
297.765957446809, 293.936170212766, 290.106382978723, 286.276595744681, 
282.446808510638, 278.617021276596, 274.787234042553, 270.957446808511, 
447.127659574468, 443.297872340426, 439.468085106383, 435.63829787234, 
431.808510638298, 427.978723404255, 424.148936170213, 420.31914893617, 
416.489361702128, 412.659574468085, 408.829787234043, 405, 401.170212765957, 
397.340425531915, 393.510638297872, 389.68085106383, 385.851063829787, 
382.021276595745, 378.191489361702, 374.36170212766, 370.531914893617, 
366.702127659574, 362.872340425532, 359.042553191489, 355.212765957447, 
351.382978723404, 347.553191489362, 343.723404255319, 339.893617021277, 
336.063829787234, 332.234042553191, 328.404255319149, 324.574468085106, 
320.744680851064, 316.914893617021, 313.085106382979, 309.255319148936, 
305.425531914894, 301.595744680851, 297.765957446808, 293.936170212766, 
290.106382978723, 286.276595744681, 282.446808510638, 278.617021276596, 
274.787234042553, 270.957446808511)), row.names = c(NA, -94L), class = "data.frame")

labelParams 的数据在这里:

list(angle = c(447.127659574468, 443.297872340426, 439.468085106383, 
435.63829787234, 431.808510638298, 427.978723404255, 424.148936170213, 
420.31914893617, 416.489361702128, 412.659574468085, 408.829787234043, 
405, 401.170212765958, 397.340425531915, 393.510638297872, 389.68085106383, 
385.851063829787, 382.021276595745, 378.191489361702, 374.36170212766, 
370.531914893617, 366.702127659575, 362.872340425532, 359.042553191489, 
355.212765957447, 351.382978723404, 347.553191489362, 343.723404255319, 
339.893617021277, 336.063829787234, 332.234042553192, 328.404255319149, 
324.574468085106, 320.744680851064, 316.914893617021, 313.085106382979, 
309.255319148936, 305.425531914894, 301.595744680851, 297.765957446809, 
293.936170212766, 290.106382978723, 286.276595744681, 282.446808510638, 
278.617021276596, 274.787234042553, 270.957446808511, 447.127659574468, 
443.297872340426, 439.468085106383, 435.63829787234, 431.808510638298, 
427.978723404255, 424.148936170213, 420.31914893617, 416.489361702128, 
412.659574468085, 408.829787234043, 405, 401.170212765957, 397.340425531915, 
393.510638297872, 389.68085106383, 385.851063829787, 382.021276595745, 
378.191489361702, 374.36170212766, 370.531914893617, 366.702127659574, 
362.872340425532, 359.042553191489, 355.212765957447, 351.382978723404, 
347.553191489362, 343.723404255319, 339.893617021277, 336.063829787234, 
332.234042553191, 328.404255319149, 324.574468085106, 320.744680851064, 
316.914893617021, 313.085106382979, 309.255319148936, 305.425531914894, 
301.595744680851, 297.765957446808, 293.936170212766, 290.106382978723, 
286.276595744681, 282.446808510638, 278.617021276596, 274.787234042553, 
270.957446808511), hjust = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
    vjust = 0.5)

编辑:无论出于何种原因,我都能够通过将 geom_point 添加到图中并将其设置为 alpha=0 来取得一些进展,这样您就看不到它了。无论出于何种原因,如果有其他几何图层,geom_text 的行为就会不同。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您能否更改 label.size 以查看是否可以找到解决方案。

    label.size=1
    

    【讨论】:

    • 如果缩小文本,就会变得难以阅读。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多