ggplot2在标记的刻度线之间显示未标记的刻度线 [英] ggplot2 displaying unlabeled tick marks between labeled tick marks

查看:383
本文介绍了ggplot2在标记的刻度线之间显示未标记的刻度线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直无法在直方图上显示较小的刻度线.我尝试过绘制未标记的主要刻度线的想法,但是刻度线不会显示.我的代码很麻烦,可能有一些多余的行.任何帮助,将不胜感激.

I've been having issues displaying minor tick marks on my histogram plot. I've tried the idea of plotting unlabeled major tick marks, but the tick marks wouldn't display. My code is pretty cumbersome and probably has some redundant lines. Any help would be appreciated.

ggplot(data=Shrimp1, aes(Shrimp1$Carapace.Length))+
geom_histogram(breaks=seq(3.5, 25, by=0.1),
             col="black",
             fill="gray", 
             alpha=1)+ 

 labs(title="Total Female Carapace Length")+
 labs(x="Carapace Length (mm)", y="# of Shrimp")+
  xlim(c(3.5, 25))+
  theme_bw()+
scale_y_continuous(expand = c(0,0),
                     limits = c(0,200))+
scale_x_continuous(breaks=seq(2.5,25,2.5))+

theme(axis.text.x=element_text(size=30,angle=45,hjust=1))+
  theme(plot.title=element_text(size=30, hjust=0.5))+
  theme(axis.text=element_text(size=30, color = "black"), 
        axis.title=element_text(size=30,face="bold"))+
  theme(panel.grid.major=element_line(colour="white"), 
        panel.grid.minor = element_line(colour = "white"))+
  theme(panel.border=element_blank())+
  theme(axis.ticks.x = (element_line(size=2)), 
        axis.ticks.y=(element_line(size=2)))+
  theme(axis.ticks.length=unit(.55, "cm"))+
  theme(panel.border=element_blank(), axis.line.x=element_line(colour="black"),
        axis.line.y=element_line(colour="black"))

存在大刻度线,但我需要在它们之间以0.1为间隔的小刻度线.

Major ticks are present but I need minor ticks between them at intervals of 0.1

推荐答案

我不认为您可以在次要休息时间中添加刻度线,但是正如您所想的那样,您可以通过在.您可以设置次要"使用带有mod(%%)的布尔索引将标签打勾为空白.

I don't think you can add ticks to minor breaks, but you can have unlabeled major ticks, as you were thinking, by labeling them explicitly in scale_x_continuous. You can set the "minor" tick labels to blank using boolean indexing with mod (%%).

类似地,如果您想要小"字样,则可以在theme中显式设置刻度大小.会打成一个不同的大小.

Similarly, you can set the tick sizes explicitly in theme if you want the "minor" ticks to be a different size.

set.seed(5)
df <- data.frame(x = rnorm(500, mean = 12.5, sd = 3))

breaks <- seq(2.5, 25, .1)

labels <- as.character(breaks)
labels[!(breaks %% 2.5 == 0)] <- ''
tick.sizes <- rep(.5, length(breaks))
tick.sizes[(breaks %% 2.5 == 0)] <- 1

df %>% 
  ggplot(aes(x)) +
  geom_histogram(binwidth = .1, color = 'black', fill = 'gray35') +
  scale_x_continuous(breaks = breaks, labels = labels, limits = c(2.5,25)) +
  theme_bw() +
  theme(panel.grid = element_blank(), 
        axis.ticks.x = element_line(size = tick.sizes))

正如您可能从我的代码中注意到的那样,您只需调用一次theme(),然后将所有主题修改都放入该调用中即可.

Also as you might notice from my code, you can just call theme() once and put all the theme modifications into that one call.

这篇关于ggplot2在标记的刻度线之间显示未标记的刻度线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆