英文:
Adjusting tick marks
问题
我想修改我的ggplot中的刻度标记,以创建一个包。然而,无论我如何努力搜索,似乎都找不到符合我的期望的解决方案(附上了一个屏幕截图,显示了我想要的外观)。
我最接近实现这个目标的方法是使用ggprism::guide_prism_bracket
,但是我遇到了一个问题,轴线没有完全填充并且存在间隙:
library(ggplot2)
year <- factor(c(2000,2005,2010,2015,2020,2025,2030,2035,2040,2045,2050))
Population <- c(6.1,6.5,6.9,7.3,7.7,8.0,8.3,8.6,8.9,9.1,9.3)
labels <- as.character(Population)
labels <- gsub(".", "," , labels, fixed=T)
df <- data.frame(year,Population,labels)
graf <- ggplot(df, aes(x= as.character(year), y=Population)) +
geom_bar(width = 0.5, stat = "identity", position=position_dodge(), color= NA) +
scale_y_continuous(expand = expansion(mult = c(0, 0))) +
ggprism::theme_prism(
base_fontface = "plain",
base_line_size = 0.7,
base_family = "Arial"
) +
scale_x_discrete(guide = ggprism::guide_prism_bracket(width = 0.15),
labels = scales::wrap_format(5))
graf
有什么建议吗?
谢谢
英文:
I want to modify the tick marks on my ggplot in order to create a package. However, no matter how diligently I search, I can't seem to find a solution that meets my expectations (attached is a screenshot depicting how I would like it to appear).
The closest I've come to achieving this was by using ggprism::guide_prism_bracket
, but I'm encountering an issue where the axis line doesn't fill in completely and contains gaps:
library(ggplot2)
year <- factor(c(2000,2005,2010,2015,2020,2025,2030,2035,2040,2045,2050))
Population <- c(6.1,6.5,6.9,7.3,7.7,8.0,8.3,8.6,8.9,9.1,9.3)
labels <- as.character(Population)
labels <- gsub("." , "," , labels, fixed=T)
df <- data.frame(year,Population,labels)
graf <- ggplot(df, aes(x= as.character(year), y=Population)) +
geom_bar(width = 0.5, stat = "identity", position=position_dodge(), color= NA) +
scale_y_continuous(expand = expansion(mult = c(0, 0))) +
ggprism::theme_prism(
base_fontface = "plain",
base_line_size = 0.7,
base_family = "Arial"
) +
scale_x_discrete(guide = ggprism::guide_prism_bracket(width = 0.15),
labels = scales::wrap_format(5))
graf
Any recommendation?
Thank you
答案1
得分: 4
可能最简洁的方法是使用geom_rug
来伪造刻度线:
ggplot(df, aes(as.numeric(as.character(year)), Population)) +
geom_col(width = 3) +
scale_x_continuous("年份", breaks = seq(1995, 2050, 5)) +
geom_rug(aes(x = as.numeric(as.character(year)) - 2.5),
outside = TRUE, sides = "b", length = unit(5, "mm")) +
coord_cartesian(expand = FALSE, clip = "off") +
theme_classic(base_size = 14) +
theme(axis.ticks.x = element_blank())
另一个不需要注释的选项是使用现有的刻度标记并移动柱形图和轴文本:
ggplot(df, aes(as.numeric(as.character(year)), Population)) +
geom_col(width = 3) +
scale_x_continuous("年份", breaks = seq(1995, 2050, 5)) +
geom_rug(aes(x = as.numeric(as.character(year)) - 2.5),
outside = TRUE, sides = "b", length = unit(5, "mm")) +
coord_cartesian(expand = FALSE, clip = "off") +
theme_classic(base_size = 14) +
theme(axis.ticks.x = element_blank())
英文:
Probably the neatest way is to fake the tick lines using geom_rug
ggplot(df, aes(as.numeric(as.character(year)), Population)) +
geom_col(width = 3) +
scale_x_continuous("Year", breaks = seq(1995, 2050, 5)) +
geom_rug(aes(x = as.numeric(as.character(year)) - 2.5),
outside = TRUE, sides = "b", length = unit(5, "mm")) +
coord_cartesian(expand = FALSE, clip = "off") +
theme_classic(base_size = 14) +
theme(axis.ticks.x = element_blank())
Another option that doesn't require annotations is to use existing tick marks and move the columns and the axis text
ggplot(df, aes(as.numeric(as.character(year)), Population)) +
geom_col(width = 3) +
scale_x_continuous("Year", breaks = seq(1995, 2050, 5)) +
geom_rug(aes(x = as.numeric(as.character(year)) - 2.5),
outside = TRUE, sides = "b", length = unit(5, "mm")) +
coord_cartesian(expand = FALSE, clip = "off") +
theme_classic(base_size = 14) +
theme(axis.ticks.x = element_blank())
答案2
得分: 2
我认为我已经非常接近了,我从theme_void
开始,并添加了一些所需的主题项。您可能需要调整一些设置。我还将年份保持为数值而不是因子。
这个想法是使您的x轴连续
- 然后调整条形的宽度
width = 2.8
,但可以根据您的喜好进行调整。 - 您展示了一个非常压缩的图形,您可以在
aspect.ratio = 0.076
进行调整。 - 关键是您希望在条形之间有刻度,并且标签位于条形下方。我决定将刻度设置为年份间隔的一半,并且不显示标签。
- 然后将“标签”作为注释添加到范围之外,结合使用
coord_cartesian
和clip = "off"
。 - 在
annotate
中,结合使用这个aspect_ratio
,y = -max(df$Population) / 5
的偏移量效果不错,但您可能需要进行微调。
代码
library(ggplot2)
ggplot(df, aes(x = year, y = Population)) +
geom_bar(width = 2.8, stat = "identity", fill = "#006096") +
theme_void() + # get rid of all, add a few below
theme(
axis.line = element_line(),
axis.ticks.x = element_line(color = "black"),
axis.line.y.right = element_line(color = "white"),
axis.ticks.length = unit(0.2, "cm"),
aspect.ratio = 0.076 # just tweak to get your "height"
) +
scale_y_continuous(expand = c(0, 0), sec.axis = sec_axis(~ .)) +
scale_x_continuous(breaks = seq(min(df$year) - 2.5, max(df$year) + 2.5, 5)) +
annotate("text", x = year, y = -max(df$Population) / 5, label = year) +
coord_cartesian(xlim = c(min(df$year), max(df$year)), ylim = c(0, max(df$Population)), clip = "off")
使用的数据
df <- data.frame(
year = c(2000,2005,2010,2015,2020,2025,2030,2035,2040,2045,2050),
Population = c(6.1,6.5,6.9,7.3,7.7,8.0,8.3,8.6,8.9,9.1,9.3)
)
英文:
I think I get pretty close, I started with theme_void
and added a few of the wanted theme items back. You might need to tweak some of the settings. I also kept year as numeric instead of a factor.
The idea is to make your x-axis continuous
- then adjust the size of your bar
width = 2.8
but feel free to tweak to your taste. - you showed a pretty squeezed down graph, you can adjust at
aspect.ratio = 0.076
- the trick is that you want ticks between the bars and the labels below the bars. I decided to set the ticks half of the year gap skewed and show no labels
- then add the "labels" as annotation outside the range in combination with
coord_cartesian
withclip = "off"
- in
annotate
in combination with thisaspect_ratio
the y offset ofy = -max(df$Population) / 5
worked out fine, but you might want to tweak that.
code
library(ggplot2)
ggplot(df, aes(x = year, y = Population)) +
geom_bar(width = 2.8, stat = "identity", fill = "#006096") +
theme_void() + # get rid of all, add a few below
theme(
axis.line = element_line(),
axis.ticks.x = element_line(color = "black"),
axis.line.y.right = element_line(color = "white"),
axis.ticks.length = unit(0.2, "cm"),
aspect.ratio = 0.076 # just tweak to get your "height"
) +
scale_y_continuous(expand = c(0, 0), sec.axis = sec_axis(~ .)) +
scale_x_continuous(breaks = seq(min(df$year) - 2.5, max(df$year) + 2.5, 5)) +
annotate("text", x = year, y = -max(df$Population) / 5, label = year) +
coord_cartesian(xlim = c(min(df$year), max(df$year)), ylim = c(0, max(df$Population)), clip = "off")
data used
df <- data.frame(
year = c(2000,2005,2010,2015,2020,2025,2030,2035,2040,2045,2050),
Population = c(6.1,6.5,6.9,7.3,7.7,8.0,8.3,8.6,8.9,9.1,9.3)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论