R 如何在ggplot2中向图形的顶部和右侧添加记号

R 如何在ggplot2中向图形的顶部和右侧添加记号,r,ggplot2,R,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){the

如何在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 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()
库(ggplot2)
图书馆(gridExtra)
x
库(ggplot2)
图书馆(gtable)
p=ggplot()+
geom_线(数据=df,aes(x=x,y=y,color='Observed'))+
geom_线(数据=df,aes(x=x,y=y2,颜色=预期值))+
实验室(x=“x”,y=“y”)+
主题_新()
#将绘图转换为grob

gt如果轴是圆木的话,这将非常有效,但它们不是。谢谢你。我已经知道了这个工具,并将其用于对数比例的图形。
scale\u x\u continuous(sides=“trbl”)
有效吗?无法测试,因为我在这台电脑上没有R。我认为sides参数不适用于scale_x_continuous()。不过还是谢谢。类似这样:您可以使用
position
参数,而不是
sides
。的可能重复
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)