调整刻度标记

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

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 进行调整
  • 诀窍是您希望在条之间有刻度线,并在条下面显示标签。我决定将刻度线设置为年份间隔的一半,并不显示标签
  • 然后在 annotate 中与 coord_cartesian 一起使用 clip = "off",在范围之外添加 "标签"
  • annotate 中与这个 aspect_ratio 结合使用 y = -max(df$Population) / 5 的y偏移量效果不错,但您可能需要微调。

代码

library(ggplot2)

ggplot(df, aes(x = year, y = Population)) +
  geom_bar(width = 2.8, stat = "identity", fill = "#006096") +
  theme_void() + # 去掉所有,添加下面的一些
  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 # 只需微调以获取您的 "高度"
  ) +
  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 with clip = &quot;off&quot;
  • in annotate in combination with this aspect_ratio the y offset of y = -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 = &quot;identity&quot;, fill = &quot;#006096&quot;) +
  theme_void() + # get rid of all, add a few below
  theme(
    axis.line = element_line(),
    axis.ticks.x = element_line(color = &quot;black&quot;),
    axis.line.y.right = element_line(color = &quot;white&quot;),
    axis.ticks.length = unit(0.2, &quot;cm&quot;),
    aspect.ratio = 0.076 # just tweak to get your &quot;height&quot;
  ) +
  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(&quot;text&quot;, 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 = &quot;off&quot;)

data used

df &lt;- 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)
)

huangapple
  • 本文由 发表于 2023年8月9日 09:34:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864033-2.html
匿名

发表评论

匿名网友

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

确定