调整刻度标记

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

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 &lt;- factor(c(2000,2005,2010,2015,2020,2025,2030,2035,2040,2045,2050))
Population &lt;- c(6.1,6.5,6.9,7.3,7.7,8.0,8.3,8.6,8.9,9.1,9.3)
labels &lt;- as.character(Population)
labels &lt;- gsub(&quot;.&quot; , &quot;,&quot; , labels, fixed=T)
df &lt;- data.frame(year,Population,labels)

graf &lt;- ggplot(df, aes(x= as.character(year), y=Population)) +
  geom_bar(width = 0.5, stat = &quot;identity&quot;, position=position_dodge(), color= NA) +
  scale_y_continuous(expand = expansion(mult = c(0, 0)))  +
  ggprism::theme_prism(
    base_fontface = &quot;plain&quot;, 
    base_line_size = 0.7, 
    base_family = &quot;Arial&quot;
  ) + 
  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(&quot;Year&quot;, breaks = seq(1995, 2050, 5)) +
  geom_rug(aes(x = as.numeric(as.character(year)) - 2.5),
           outside = TRUE, sides = &quot;b&quot;, length = unit(5, &quot;mm&quot;)) +
  coord_cartesian(expand = FALSE, clip = &quot;off&quot;) +
  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(&quot;Year&quot;, breaks = seq(1995, 2050, 5)) +
  geom_rug(aes(x = as.numeric(as.character(year)) - 2.5),
           outside = TRUE, sides = &quot;b&quot;, length = unit(5, &quot;mm&quot;)) +
  coord_cartesian(expand = FALSE, clip = &quot;off&quot;) +
  theme_classic(base_size = 14) +
  theme(axis.ticks.x = element_blank())

调整刻度标记

答案2

得分: 2

我认为我已经非常接近了,我从theme_void开始,并添加了一些所需的主题项。您可能需要调整一些设置。我还将年份保持为数值而不是因子。

调整刻度标记

这个想法是使您的x轴连续

  • 然后调整条形的宽度width = 2.8,但可以根据您的喜好进行调整。
  • 您展示了一个非常压缩的图形,您可以在aspect.ratio = 0.076进行调整。
  • 关键是您希望在条形之间有刻度,并且标签位于条形下方。我决定将刻度设置为年份间隔的一半,并且不显示标签。
  • 然后将“标签”作为注释添加到范围之外,结合使用coord_cartesianclip = "off"
  • annotate中,结合使用这个aspect_ratioy = -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 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.html
匿名

发表评论

匿名网友

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

确定