在 ggplot 中调整棒棒糖图的行高。

huangapple go评论70阅读模式
英文:

adjusting row height in ggplot lollipop

问题

我需要一些帮助来格式化我的棒棒糖图表。
每一行之间的间距太大,每一行中有太多的空白,因此顶部和底部的线太靠近图表的边缘。
我尝试调整绘图和主题中的一些格式设置,但似乎一直在循环中。
任何帮助都将不胜感激。
谢谢

> dput(data)
structure(list(Type = c("Programming", "Programming", "Analytics", 
"Professional", "Applied Science", "Applied Science", "Programming", 
"Analytics", "Professional", "Programming", "Analytics", "Professional", 
"Applied Science", "Analytics", "Professional", "Applied Science"
), Skill = c("Python", "Other", "Databases", "Project Management", 
"NGS", "Environmental Engineering", "Perl", "Bioinformatics", 
"Teaching", "R", "Statistics", "Supervision", "Microbiome", "Visualization", 
"Science Communication", "Applied Microbiology"), Of.5 = c(4, 
4, 4.5, 4.5, 4, 4, 4.5, 4.5, 4.5, 4.7, 4.5, 4.5, 4.5, 4.7, 4.5, 
4.7), Of.10 = c(7.5, 8, 8, 8, 8, 8, 8.5, 8.5, 8.5, 9, 9, 9, 9, 
9.5, 9.5, 9.5)), class = "data.frame", row.names = c(NA, -16L
))

data<-datAnal_skills

skills_lolli_plot_corrOrder<-function(data){
  ## reordering data within each facet
  dat2 <- data %>%
    # 1. Remove grouping
    ungroup() %>%
    # 2. Arrange by
    #   i.  facet group (tissue)
    #   ii. value (score)
    arrange(Type, Of.10) %>%
    # 3. Add order column of row numbers
    mutate(order = row_number())
  ###
  ggplot(dat2, aes(order, Of.10)) +
    geom_segment(aes(xend = order, yend = 0), 
                 colour = "grey",lwd=1.5) +
    geom_point(size = 8, col = "#f39c12") + 
    geom_text(aes( label = Of.10),  size = 4,color="white")+
    ylim(c(0,10))+
    scale_x_continuous(
      breaks = dat2$order,
      labels = dat2$Skill) +
    coord_flip()+
    facet_wrap(~Type,scales='free_y')+
    labs(
      x = "",
      y = "",
      title = ""
    ) +
    theme_bw() +
    theme(
      axis.text.x=element_blank(),
      axis.ticks = element_blank(),
      panel.grid.major.x = element_blank(),
      panel.grid.minor.x = element_blank(),
      panel.grid.major.y = element_line(colour = "grey60", linetype = "dashed"),
      axis.text.y = element_text(size=14),legend.text=element_text(size=14),strip.text=element_text(size=14)          # increase label text size
    ) 
}

在 ggplot 中调整棒棒糖图的行高。


<details>
<summary>英文:</summary>
I need some help formatting my lollipop graph.
The rows are too far apart from each other, with too much white space in each row therefore the top and bottom lines are too close to the edges of the plot.
Ive tried adjusting a few formatting things in the plotting and the theme, but seem to be going in circles.  
Any help appreciated.
Thanks
&gt; dput(data)
structure(list(Type = c(&quot;Programming&quot;, &quot;Programming&quot;, &quot;Analytics&quot;, 
&quot;Professional&quot;, &quot;Applied Science&quot;, &quot;Applied Science&quot;, &quot;Programming&quot;, 
&quot;Analytics&quot;, &quot;Professional&quot;, &quot;Programming&quot;, &quot;Analytics&quot;, &quot;Professional&quot;, 
&quot;Applied Science&quot;, &quot;Analytics&quot;, &quot;Professional&quot;, &quot;Applied Science&quot;
), Skill = c(&quot;Python&quot;, &quot;Other&quot;, &quot;Databases&quot;, &quot;Project Management&quot;, 
&quot;NGS&quot;, &quot;Environmental Engineering&quot;, &quot;Perl&quot;, &quot;Bioinformatics&quot;, 
&quot;Teaching&quot;, &quot;R&quot;, &quot;Statistics&quot;, &quot;Supervision&quot;, &quot;Microbiome&quot;, &quot;Visualization&quot;, 
&quot;Science Communication&quot;, &quot;Applied Microbiology&quot;), Of.5 = c(4, 
4, 4.5, 4.5, 4, 4, 4.5, 4.5, 4.5, 4.7, 4.5, 4.5, 4.5, 4.7, 4.5, 
4.7), Of.10 = c(7.5, 8, 8, 8, 8, 8, 8.5, 8.5, 8.5, 9, 9, 9, 9, 
9.5, 9.5, 9.5)), class = &quot;data.frame&quot;, row.names = c(NA, -16L
))
data&lt;-datAnal_skills
skills_lolli_plot_corrOrder&lt;-function(data){
## reordering data within each facet
dat2 &lt;- data %&gt;% 
# 1. Remove grouping
ungroup() %&gt;% 
# 2. Arrange by
#   i.  facet group (tissue)
#   ii. value (score)
arrange(Type, Of.10) %&gt;%
# 3. Add order column of row numbers
mutate(order = row_number())
###
ggplot(dat2, aes(order, Of.10)) +
geom_segment(aes(xend = order, yend = 0), 
colour = &quot;grey&quot;,lwd=1.5) +
geom_point(size = 8, col = &quot;#f39c12&quot;) + 
geom_text(aes( label = Of.10),  size = 4,color=&quot;white&quot;)+
ylim(c(0,10))+
scale_x_continuous(
breaks = dat2$order,
labels = dat2$Skill) +
coord_flip()+
facet_wrap(~Type,scales=&#39;free_y&#39;)+
labs(
x = &quot;&quot;,
y = &quot;&quot;,
title = &quot;&quot;
) +
theme_bw() +
theme(
axis.text.x=element_blank(),
axis.ticks = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(colour = &quot;grey60&quot;, linetype = &quot;dashed&quot;),
axis.text.y = element_text(size=14),legend.text=element_text(size=14),strip.text=element_text(size=14)          # increase label text size
) 
}
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/jgGmZ.png
</details>
# 答案1
**得分**: 1
你可以使用`expand = expansion(0, 0.5)`来扩展比例尺,放在`scale_x_continuous()`函数内部:
```r
skills_lolli_plot_corrOrder<-function(data){
## 在每个分面内重新排序数据
dat2 <- data %>%
# 1. 删除分组
ungroup() %>%
# 2. 排序
#   i.  按 facet 组(组织)
#   ii. 值(分数)
arrange(Type, Of.10) %>%
# 3. 添加行号的排序列
mutate(order = row_number())
###
ggplot(dat2, aes(order, Of.10)) +
geom_segment(aes(xend = order, yend = 0), 
colour = "grey",lwd=1.5) +
geom_point(size = 8, col = "#f39c12") + 
geom_text(aes( label = Of.10),  size = 4,color="white")+
ylim(c(0,10))+
scale_x_continuous(
breaks = dat2$order,
labels = dat2$Skill,
expand = expansion(0, 0.5)) +
coord_flip()+
facet_wrap(~Type,scales='free_y')+
labs(
x = "",
y = "",
title = ""
) +
theme_bw() +
theme(
axis.text.x=element_blank(),
axis.ticks = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(colour = "grey60", linetype = "dashed"),
axis.text.y = element_text(size=14),legend.text=element_text(size=14),strip.text=element_text(size=14)          # 增加标签文字大小
) 
}

这将产生:

在 ggplot 中调整棒棒糖图的行高。

你可以尝试调整expansion()函数中的值来得到你想要的效果。

英文:

You can expand the scale using, e.g. expand = expansion(0, 0.5) inside the scale_x_continuous() function:

skills_lolli_plot_corrOrder&lt;-function(data){
  ## reordering data within each facet
  dat2 &lt;- data %&gt;% 
    # 1. Remove grouping
    ungroup() %&gt;% 
    # 2. Arrange by
    #   i.  facet group (tissue)
    #   ii. value (score)
    arrange(Type, Of.10) %&gt;%
    # 3. Add order column of row numbers
    mutate(order = row_number())
  ###
  ggplot(dat2, aes(order, Of.10)) +
    geom_segment(aes(xend = order, yend = 0), 
                 colour = &quot;grey&quot;,lwd=1.5) +
    geom_point(size = 8, col = &quot;#f39c12&quot;) + 
    geom_text(aes( label = Of.10),  size = 4,color=&quot;white&quot;)+
    ylim(c(0,10))+
    scale_x_continuous(
      breaks = dat2$order,
      labels = dat2$Skill,
      expand = expansion(0, 0.5)) +
    coord_flip()+
    facet_wrap(~Type,scales=&#39;free_y&#39;)+
    labs(
      x = &quot;&quot;,
      y = &quot;&quot;,
      title = &quot;&quot;
    ) +
    theme_bw() +
    theme(
      axis.text.x=element_blank(),
      axis.ticks = element_blank(),
      panel.grid.major.x = element_blank(),
      panel.grid.minor.x = element_blank(),
      panel.grid.major.y = element_line(colour = &quot;grey60&quot;, linetype = &quot;dashed&quot;),
      axis.text.y = element_text(size=14),legend.text=element_text(size=14),strip.text=element_text(size=14)          # increase label text size
    ) 
}

which gives:

在 ggplot 中调整棒棒糖图的行高。

You can play around with the values inside the expansion() function to get exactly what you want.

huangapple
  • 本文由 发表于 2023年5月28日 21:23:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定