如何在ggplot2的图表的顶部和右侧添加刻度 [英] How do I add ticks to the top and right side of a graph in ggplot2
本文介绍了如何在ggplot2的图表的顶部和右侧添加刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在ggplot2的图表的顶部和右侧如何添加刻度?我试图满足一个图形的标准格式,并且已经有了很长的主题。
下面是我使用一些任意数据的例子:
library(ggplot2)
library(gridExtra)
x< - seq(1,10,1 )
y < - seq(1,100,10)
y2 < - seq(100,1,-10)
df < - data.frame(x,y,y2)
theme_new< - function(base_size = 12){theme(
plot.title = element_text(vjust = 3,size = 16,family =serif),#plot title attrib。
plot.margin = unit(c(8,4,8,4),lines),#plot margin
panel.border = element_rect(color =black,fill = F), #axis color = black
panel.grid.major = element_blank(),#remove major grid
panel.grid.minor = element_blank(),#remove minor grid
panel.background = element_rect (fill =white),#背景颜色
legend.background = element_rect(fill =white),#背景颜色
#legend.justification = c(0,0),#锁定点为乐gend
#legend.position = c(0,1),#将图例内置于绘图区域,使用=none关闭
legend.key = element_blank(),#关闭图例中的符号周围的矩形
legend.title = element_blank(),#关闭图例标题
legend.text = element_text(size = 12),#设置图例文本的属性
axis.title.x = element_text(vjust = -2,size = 14,family =serif),#改变轴标题
axis.title.y = element_text(vjust = 1,angle = 90, size = 14,family =serif),#改变轴标题
axis.text.x = element_text(size = 12,vjust = -0.25,color =black,family =serif), #更改轴标签字体属性
axis.text.y = element_text(size = 12,hjust = 1,color =black,family =serif),#更改轴标签字体属性$ b $ #axis.ticks = element_line(color =black,size = 0.5),#设置轴刻度的厚度和颜色
axis.ticks.length =单位(-0.25,cm),#-ve长度=内部刻度
axis.ticks.margin =单位(0.5,cm)#刻度和文本之间的差值
)}
ggplot()+ $ b $ (x = x,y = y,color ='Observed'))+
geom_line(data = df,aes(x = x,y = y2,color ='Expected' ))+
labs(x =X,y =Y)+
theme_new()
这产生了这种结果:
我需要的是这个(在这个例子中是在Illustrator中修改的):
< img src =https://i.stack.imgur.com/jQ8JV.pngalt =plot example modified>
对此有任何帮助都非常感谢。
谢谢。
解决方案
library(ggplot2)
library(gtable)
p = ggplot()+
geom_line(data = df,aes(x = x,y = y,color ='观察'))+
geom_line(data = df,aes(x = x,y = y2,color ='Expected '))+
labs(x =X,y =Y)+
theme_new()
转换图为grob
gt< - ggplotGrob(p)
#获取面板在布局
面板中的位置< -c(子集(gt $ layout,name ==)面板,se = t:r))
##对于底部轴
#获取布局中底部轴的行号
rn< - which(gt $ layout $ name ==axis-b)
#提取轴(仅限刻度线)
axis.grob< - gt $ grobs [[rn]]
轴b< - axis.grob $ children [[2]]#两个孩子 - 获得第二个
axisb#注意:两个grobs - 刻度线和文本
#获取刻度标记
xaxis = axisb $ grobs [[1]]#注意:首先勾选标记
xaxis $ y = xaxis $ y - 单位(0.25,cm)#将它们置于面板内
#向gt添加一个新行,并将修改后的xaxis grob插入到新行中。
gt < - gtable_add_rows(gt,单元(0,lines),panel $ t-1)
gt < - gtable_add_grob(gt,xaxis,l = panel $ l,t = panel $ t,r = panel $ r,name =ticks)
##重复左轴
#获取布局中左轴的行号
面板< -c(子集(gt $布局,名称==面板,se = t:r))
rn< - 其中(GT $ layout $ name ==axis-l)
#提取轴(刻度线和轴文本)
axis.grob< - gt $ grobs [[rn]]
axisl< - axis.grob $ children [ [2]]#两个孩子 - 获得第二个
axisl#注意:两个grobs - 文本和刻度标记
#获取刻度标记
yaxis = axisl $ grobs [[[ 2]]#注意:第二个刻度线为
yaxis $ x = yaxis $ x - 单位(0.25,cm)#将它们放置在面板内
#添加一个新列到gt ,并将修改后的yaxis grob插入新列。
gt < - gtable_add_cols(gt,单元(0,lines),panel $ r)
gt < - gtable_add_grob(gt,yaxis,t = panel $ t,l = panel $ r +1,name =ticks)
#关闭
gt $布局[gt $ layout $ name ==ticks,] $ clip =off
#绘制
grid.draw(gt)
How can ticks be added to the top and right side of a graph in ggplot2? I'm trying to meet a standard format for graphs and already have a lengthy theme.
Here is an example of what I have using some arbitrary data:
library (ggplot2)
library (gridExtra)
x <- seq(1,10,1)
y <- seq(1,100,10)
y2 <- seq(100,1,-10)
df <- data.frame(x,y,y2)
theme_new <- function(base_size = 12){theme(
plot.title = element_text (vjust = 3, size = 16,family="serif"), # plot title attrib.
plot.margin = unit (c(8, 4, 8, 4), "lines"), # plot margins
panel.border = element_rect (colour = "black", fill = F), # axis colour = black
panel.grid.major = element_blank (), # remove major grid
panel.grid.minor = element_blank (), # remove minor grid
panel.background = element_rect (fill = "white"), # background colour
legend.background = element_rect (fill = "white"), # background colour
#legend.justification=c(0, 0), # lock point for legend
#legend.position = c(0, 1), # put the legend INSIDE the plot area, use = "none" to turn off
legend.key = element_blank (), # switch off the rectangle around symbols in the legend
legend.title = element_blank (), # switch off the legend title
legend.text = element_text (size = 12), # sets the attributes of the legend text
axis.title.x = element_text (vjust = -2, size = 14,family="serif"), # change the axis title
axis.title.y = element_text (vjust = 1, angle = 90, size = 14,family="serif"), # change the axis title
axis.text.x = element_text (size = 12, vjust = -0.25, colour = "black",family="serif"),# change the axis label font attributes
axis.text.y = element_text (size = 12, hjust = 1, colour = "black",family="serif"), # change the axis label font attributes
axis.ticks = element_line (colour = "black", size = 0.5), # sets the thickness and colour of axis ticks
axis.ticks.length = unit(-0.25 , "cm"), # -ve length = inside ticks
axis.ticks.margin = unit(0.5, "cm") # margin between the ticks and the text
)}
ggplot() +
geom_line(data=df, aes(x=x, y=y, color='Observed')) +
geom_line(data=df, aes(x=x, y=y2, color='Expected')) +
labs(x="X", y="Y") +
theme_new()
Which produces this:
What I need is this (modified in Illustrator for this example):
Any help with this is greatly appreciated.
Thanks.
解决方案
library(ggplot2)
library(gtable)
p = ggplot() +
geom_line(data=df, aes(x=x, y=y, color='Observed')) +
geom_line(data=df, aes(x=x, y=y2, color='Expected')) +
labs(x="X", y="Y") +
theme_new()
# Convert the plot to a grob
gt <- ggplotGrob(p)
# Get the position of the panel in the layout
panel <-c(subset(gt$layout, name=="panel", se=t:r))
## For the bottom axis
# Get the row number of the bottom axis in the layout
rn <- which(gt$layout$name == "axis-b")
# Extract the axis (tick marks only)
axis.grob <- gt$grobs[[rn]]
axisb <- axis.grob$children[[2]] # Two children - get the second
axisb # Note: two grobs - tick marks and text
# Get the tick marks
xaxis = axisb$grobs[[1]] # NOTE: tick marks first
xaxis$y = xaxis$y - unit(0.25, "cm") # Position them inside the panel
# Add a new row to gt, and insert the revised xaxis grob into the new row.
gt <- gtable_add_rows(gt, unit(0, "lines"), panel$t-1)
gt <- gtable_add_grob(gt, xaxis, l = panel$l, t = panel$t, r = panel$r, name = "ticks")
## Repeat for the left axis
# Get the row number of the left axis in the layout
panel <-c(subset(gt$layout, name=="panel", se=t:r))
rn <- which(gt$layout$name == "axis-l")
# Extract the axis (tick marks and axis text)
axis.grob <- gt$grobs[[rn]]
axisl <- axis.grob$children[[2]] # Two children - get the second
axisl # Note: two grobs - text and tick marks
# Get the tick marks
yaxis = axisl$grobs[[2]] # NOTE: tick marks second
yaxis$x = yaxis$x - unit(0.25, "cm") # Position them inside the panel
# Add a new column to gt, and insert the revised yaxis grob into the new column.
gt <- gtable_add_cols(gt, unit(0, "lines"), panel$r)
gt <- gtable_add_grob(gt, yaxis, t = panel$t, l = panel$r+1, name = "ticks")
# Turn clipping off
gt$layout[gt$layout$name == "ticks", ]$clip = "off"
# Draw it
grid.draw(gt)
这篇关于如何在ggplot2的图表的顶部和右侧添加刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文