怎么用R语言ggplot2画图及一套好看的配色以及调整字体

发布时间:2021-11-22 15:52:38 作者:柒染
来源:亿速云 阅读:539
# 怎么用R语言ggplot2画图及一套好看的配色以及调整字体

## 一、ggplot2基础绘图入门

### 1.1 安装与加载
```r
install.packages("ggplot2")  # 首次使用需安装
library(ggplot2)             # 加载包

1.2 基本语法结构

ggplot2采用图层叠加的绘图逻辑:

ggplot(data = <数据集>) + 
  <几何对象>(aes(<美学映射>)) + 
  <标度> + 
  <主题> + 
  <其他组件>

1.3 常用几何对象

函数 图形类型 示例
geom_point() 散点图 aes(x, y)
geom_line() 折线图 aes(x, y, group)
geom_bar() 柱状图 aes(x, fill)
geom_boxplot() 箱线图 aes(x, y)
geom_histogram() 直方图 aes(x)

二、高级配色方案

2.1 内置配色方案

# 使用RColorBrewer调色板
library(RColorBrewer)
ggplot(mtcars, aes(x=cyl, fill=factor(cyl))) + 
  geom_bar() +
  scale_fill_brewer(palette = "Set2")

# viridis色盲友好配色
library(viridis)
ggplot(diamonds, aes(x=carat, y=price, color=cut)) +
  geom_point() +
  scale_color_viridis(discrete = TRUE)

2.2 自定义配色

# 定义颜色向量
my_palette <- c("#1f77b4", "#ff7f0e", "#2ca02c", 
                "#d62728", "#9467bd", "#8c564b")

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
  geom_point(size=3) +
  scale_color_manual(values = my_palette)

2.3 渐变配色

# 连续型变量渐变
ggplot(faithfuld, aes(waiting, eruptions, fill=density)) +
  geom_tile() +
  scale_fill_gradientn(colors = terrain.colors(10))

三、字体精细调整

3.1 系统字体设置

library(showtext)
font_add("heiti", "simhei.ttf")  # 添加中文字体
showtext_auto()                  # 自动启用

ggplot(mtcars, aes(x=wt, y=mpg)) +
  geom_point() +
  labs(title="汽车重量与油耗关系") +
  theme(text=element_text(family="heiti"))

3.2 主题元素调整

ggplot(iris, aes(x=Species, y=Sepal.Length)) +
  geom_boxplot() +
  theme(
    plot.title = element_text(size=16, face="bold", hjust=0.5),
    axis.title = element_text(size=12),
    axis.text = element_text(angle=45, vjust=1, hjust=1),
    legend.title = element_text(color="blue", size=10)
  )

3.3 使用预置主题

library(ggthemes)
ggplot(economics, aes(date, unemploy)) +
  geom_line() +
  theme_economist() +       # 经济学人风格
  scale_fill_economist()

四、完整案例演示

4.1 数据准备

library(gapminder)
data <- gapminder %>% 
  filter(year == 2007) %>% 
  mutate(gdp_per_cap = gdpPercap/1000)

4.2 绘制高级图形

ggplot(data, aes(x=gdp_per_cap, y=lifeExp, 
                size=pop, color=continent)) +
  geom_point(alpha=0.7) +
  scale_size(range = c(2, 12), name="人口(亿)") +
  scale_color_manual(values = c("#66c2a5", "#fc8d62", 
                               "#8da0cb", "#e78ac3", "#a6d854")) +
  labs(title="2007年各国经济发展与预期寿命",
       x="人均GDP(千美元)", 
       y="预期寿命(岁)") +
  theme_minimal(base_size=12) +
  theme(
    plot.title = element_text(size=18, face="bold", hjust=0.5),
    legend.position = "bottom",
    panel.grid.minor = element_blank(),
    text = element_text(family="Arial")
  ) +
  guides(color = guide_legend(nrow=1))

五、实用技巧补充

  1. 多图排版:使用patchwork

    library(patchwork)
    p1 <- ggplot(...)  # 第一个图
    p2 <- ggplot(...)  # 第二个图
    p1 + p2 + plot_layout(ncol=2)
    
  2. 交互式图形:转换为plotly

    library(plotly)
    ggplotly(p)  # p为ggplot对象
    
  3. 保存高清图片

    ggsave("plot.png", width=10, height=6, dpi=300)
    

六、常见问题解决

  1. 中文显示问题

    • 确保系统已安装对应字体
    • 使用showtextextrafont
  2. 图例重叠

    theme(legend.position = "bottom",
         legend.box = "horizontal")
    
  3. 坐标轴标签过长

    scale_x_discrete(labels = function(x) str_wrap(x, width=10))
    

通过本文介绍的ggplot2绘图方法、配色方案和字体调整技巧,您可以创建出版级质量的统计图形。建议多练习ggplot2的官方文档和R Graph Gallery中的案例来提升可视化能力。 “`

(注:实际字数约1500字,此处为缩略版本。完整版应包含更多示例代码、效果截图和详细解释)

推荐阅读:
  1. R语言笔记 barplot画图
  2. R语言笔记 plot 画图

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

r语言 ggplot2

上一篇:R语言怎么调整图的位置

下一篇:c语言怎么实现含递归清场版扫雷游戏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》