自定义x轴刻度 [英] Customise x-axis ticks
问题描述
10,20,30,40,50,51,52,53,54,55,56,57,58,58,60,70,80,90,... 190200。
我知道,我可以通过 scale_x_continuous
通过使用间歇
和 minor_breaks
。但是,我没有得到预期的输出。这里是我的尝试:(注意:已经创建了数据,例如我的数据非常大)。 $ b -----
x <-seq(1:200)
y< -seq(51,250,by = 1)
df< -data.frame(x = x, y = y)
code :(更新基于@Didzis Elferts建议)
-----
ggplot(data = df,aes(x,y))+ geom_line(size = 1.6)+
scale_x_continuous(break = c(10,20,30,40,seq(50,60,by = 2),seq(70,200,10))
,minor_breaks = seq(50,60,by = 2))+
+ theme(axis.text.x = element_text(size = 16),axis.text.y = element_text(size = 16))+
+ theme(axis.title.x = element_text(size = 16),axis.title.y = element_text(size = 16))+
+ theme(axis.ticks.x = element_line(size = 1)) + xlab(Time)+ ylab(value)
+ theme(axis.ticks.length = unit(0.8,cm))
根据以下建议,现在唯一的问题是在50-60的间隔内X轴的值没有明显出现。
有任何建议要说清楚!
minor_breaks =
,您可以设置次要网格线。要在x轴下设置数字,所有数字应该提供参数 breaks =
。 ggplot(data = df,aes(x,y))+ geom_line(size = 1.6)+
scale_x_continuous(breaks = c(10,20,30,40,seq(50,60,by = 1),seq(70,200,10)),
minor_breaks = seq(50,60,by = 1))
第二个问题 - 在 theme()中设置
- 这使得您的轴线变宽,因此它们显示为小矩形。如果你想让轴线更长时间使用 axis.ticks.x = element_line(size = 5)
axis.ticks.length =
。
+ theme(axis.ticks.length = unit(0.5,cm))
I have a very large data frame (2 columns) in terms of records. I have plotted the graph in ggplot2. The X axis is the time and the Y axis is values. Over a specific interval from time 50 to 60, I want to make the ticks increments to be smaller such as (50,51,51,53,...59,60). For the rest of the axis, it is fine to have the ticks incremented by 10. So,I would expect to have X-axis values like :
10,20,30,40,50,51,52,53,54,55,56,57,58,58,60,70,80,90,..190,200.
I know, I can modify the ticks through scale_x_continuous
by using breaks
and minor_breaks
. However, I'm not getting the expected output. Here is my try:(note: have created data just for example as my data is very large).
data:
-----
x<-seq(1:200)
y<-seq(51,250,by=1)
df<-data.frame(x=x,y=y)
code:(Update based on @Didzis Elferts suggestion)
-----
ggplot(data=df, aes(x,y))+geom_line(size=1.6)+
scale_x_continuous( breaks=c(10,20,30,40,seq(50,60,by=2),seq(70,200,10))
,minor_breaks=seq(50,60,by=2) )+
+theme(axis.text.x=element_text(size=16),axis.text.y=element_text(size=16))+
+theme(axis.title.x=element_text(size=16),axis.title.y=element_text(size=16))+
+theme(axis.ticks.x = element_line(size = 1))+xlab("Time")+ylab("value")
+theme(axis.ticks.length=unit(0.8,"cm"))
Based on the suggestion below, the only problem now is the values of X-axis during the interval between 50-60 is not clearly appeared. Any suggestions to make it clear!!
With argument minor_breaks=
you set the minor gridlines. To set numbers under the x axis all number should be provided with argument breaks=
.
ggplot(data=df, aes(x,y))+geom_line(size=1.6)+
scale_x_continuous(breaks=c(10,20,30,40,seq(50,60,by=1),seq(70,200,10)),
minor_breaks=seq(50,60,by=1))
For the second problem - you set axis.ticks.x=element_line(size=5)
inside theme()
- that makes your axis ticks wider so they appear as small rectangles. If you want to make axis ticks longer use axis.ticks.length=
.
+theme(axis.ticks.length=unit(0.5,"cm"))
这篇关于自定义x轴刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!