英文:
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
)
}
<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
> 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
)
}
[![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) # 增加标签文字大小
)
}
这将产生:
你可以尝试调整expansion()
函数中的值来得到你想要的效果。
英文:
You can expand the scale using, e.g. expand = expansion(0, 0.5)
inside the scale_x_continuous()
function:
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,
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) # increase label text size
)
}
which gives:
You can play around with the values inside the expansion()
function to get exactly what you want.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论