R语言ggplot2修改y轴标签朝内、箭头坐标轴

修改刻度朝内
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) + 
  geom_point(aes(color = drv, shape = drv)) + 
  theme_bw() + 
  theme(axis.ticks.length.y = unit(-0.15, 'cm'), #设置y轴的刻度长度为负数,即向内
        axis.text.y = element_text(margin = unit(c(0.5, 0.5, 0.5, 0.5), 'cm'))) #设置y轴标签距离y轴的距离
image.png
稍微美化
ggplot(mpg, aes(displ, hwy)) + 
  geom_point(aes(color = drv, shape = drv)) + 
  labs(title = 'Engine Displacement VS. Highway Miles per Gallon',
       color = 'Type of Drives', 
       shape = 'Type of Drives') + 
  theme_bw() + 
  theme(axis.ticks.length.y = unit(-0.15, 'cm'),
    axis.text.y = element_text(margin = unit(c(0.5, 0.5, 0.5, 0.5), 'cm'))) + 
  theme(plot.title = element_text(hjust = 0.5, face = 4, color = 'darkred', size = rel(1.5)),
        legend.title = element_text(face = 2),
        axis.title = element_text(face = 'bold')) + 
  scale_shape_discrete(breaks = c('4', 'f', 'r'),
                       labels = c('Four-Wheel Drive', 'Front-Wheel Drive', 'Rear- Wheel Drive')) + 
  scale_color_discrete(breaks = c('4', 'f', 'r'),
                      labels = c('Four-Wheel Drive', 'Front-Wheel Drive', 'Rear- Wheel Drive'))
image.png

修改y轴刻度朝内,设置xy轴尾部为箭头

ggplot(mpg, aes(x = hwy)) + 
    geom_histogram(bins = 15) + 
    theme_classic() + 
    theme(axis.ticks.length.y = unit(-0.15, 'cm'),
          axis.text.y = element_text(margin = unit(c(0.5, 0.5, 0.5, 0.5), 'cm'))) +  #修改y轴刻度朝内
    theme(axis.line = element_line(arrow = arrow(length = unit(0.5, 'cm'))))  #坐标轴尾端为箭头
image.png

你可能感兴趣的:(R语言ggplot2修改y轴标签朝内、箭头坐标轴)