R绘图 | 描述性统计常用图(散点图+柱状图+饼图)

简介: R绘图 | 描述性统计常用图(散点图+柱状图+饼图)

绘制


数据处理

library('tidyverse')
# install.packages('janitor')
library('janitor')
library('readxl')
library('lubridate')
ppa_price <- read_excel(
  "2021_utility-scale_solar_data_update_0.xlsm", 
  sheet = "PPA Price by Project (PV only)", 
  range = "A24:L357"
)
str(ppa_price)
# 长宽转换
ppa_price_long <- ppa_price %>% 
  pivot_longer(cols = c(CAISO:Hawaii),
               names_to = "region", 
               values_to = "price", 
               values_drop_na = TRUE)
# 列名清洗
ppa_price_long <- ppa_price_long %>%
  clean_names() 
# Convert Region to Factor 
ppa_price_long <- ppa_price_long %>%
  mutate(region_cat = factor(region, ordered = TRUE))
# 提取年份
str(ppa_price_long)
ppa_price_long <- ppa_price_long %>% 
  mutate_if(is.POSIXct, as_date) %>%  # 日期格式为POSIXct
  mutate(ppa_year = year(ppa_execution_date))

散点图

# 颜色设置
color.pal <- c(
  "#4E79A7", #dark blue
  "#F28E2B", #orange
  "#E15759", #red
  "#76B7B2", #teal
  "#59A14F", #green
  "#BAB0AC", #gray
  "#B07AA1", #purple
  "#FF9DA7", #pink
  "#9C755F", #brown
  "#EDC948" #yellow
)
## 散点图
p1 <- ppa_price_long %>% 
  ggplot(aes(x = ppa_execution_date,
             y = price,
             size = capacity_mw,
             color = region))+
  geom_point(shape = 1, stroke = 1.2)+
  scale_size(guide = "none")+
  scale_color_manual(values = color.pal, name = "Region")+
  #ggthemes::scale_color_tableau()+
  scale_x_date(date_breaks = "2 years", date_labels = "%Y")+ # x轴日期间距及格式
  ylab("PPA Price (2020 $/MWh)")+
  theme_light()+
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.border = element_blank(),
        axis.line.x.bottom = element_line(color = "lightgray"),
        axis.line.y.left = element_line(color = "lightgray"),
        axis.title.y = element_text(size = 10),
        axis.title.x = element_blank())+
  guides(color = guide_legend(
    override.aes=list(shape = 15)))

p1

image.png


p1

堆叠柱状图


## 堆叠柱状图
year_totals <- ppa_price_long %>% 
  group_by(ppa_year) %>% 
  arrange(ppa_year, region) %>% 
  summarise(year_total = prettyNum(trunc(sum(capacity_mw)),
                                   big.mark = ","),
            region = first(region))
p2_data = ppa_price_long %>% 
  group_by(region, ppa_year) %>% 
  summarise(capacity_mw = sum(capacity_mw)) %>% 
  left_join(year_totals)
p2 <- p2_data %>% 
  ggplot(aes(x = ppa_year,
             y = capacity_mw,
             color = region,
             fill = region,
             label = year_total))+
  geom_col(width = 0.7)+
  geom_text(position = position_stack(), vjust = -0.5, size = 3) +
  scale_fill_manual(values = color.pal)+
  scale_color_manual(values = color.pal)+
  scale_x_continuous(breaks = seq(2006, 2021, 1))+
  scale_y_continuous(limits = c(0, 3500), 
                     expand = c(0,0),
                     labels = c("0K", "1K", "2K", "3K"),
                     breaks = c(0, 1000, 2000, 3000))+
  ylab("Capacity (MW-AC)")+
  theme_light()+
  theme(legend.position = "top",
        axis.title.y = element_text(size = 10),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.border = element_blank(),
        axis.line.x.bottom = element_line(color = "lightgray"),
        axis.line.y.left = element_line(color = "lightgray"),
        axis.title.x = element_blank(),
        axis.text.x = element_text(angle = 90))

p2

image.png


p2

饼图


cumulative_cap <- ppa_price_long %>% 
  group_by(region) %>% 
  summarise(cumulative_capacity = round(sum(capacity_mw))) %>% 
  mutate(capacity_label = prettyNum(cumulative_capacity, big.mark = ","))
p3 <- ggplot(cumulative_cap,
             aes(x="",
                 y=cumulative_capacity,
                 fill=region))+
  geom_bar(stat="identity", width=1)+
  scale_fill_manual(values = color.pal)+
  coord_polar("y", start=0, direction = -1)+
  annotate("text", y = 4000, x = 1,
           label = "West (non-ISO)\n 7,919", size = 3)+
  annotate("text", y = 18000, x = 1.1,
           label = "CAISO \n 8,649", color = "white", size = 3)+
  annotate("text", y = 8800, x = 1.6,
           label = "Southwest (non-ISO) \n 2,538", size = 3)+
  annotate("text", y = 13000, x = 1.7,
           label = "ISO-NE \n 326", size = 3)+
  annotate("text", y = 11000, x = 1.7,
           label = "PJM \n 739", size = 3)+
  theme_void()+
  labs(caption = "Cumulative Capacity (MW-AC)")+
  theme(legend.position="top",
        legend.title = element_blank(),
        legend.key.size = unit(.5, 'cm'),
        plot.caption = element_text(hjust = .5),
        plot.title = element_text(hjust = 0.5, size = 12),
        plot.subtitle = element_text(hjust = 0.5, size = 10))+
  guides(fill = guide_legend(nrow = 5, byrow = TRUE))
p3

image.png


p3

拼图

library('ggpubr')
p_full <- ggarrange(
  ggarrange(p1, p2, nrow=2, legend = "none"), 
  p3, 
  widths = c(2, 1), heights = c(1,1))
p_full
annotate_figure(p_full,
                top = text_grob(
                  "Utility-Scale Solar \n Power Purchase Agreement Prices for PV", 
                  face = "bold",
                  size = 14, 
                  color = color.pal[1]),
                bottom = text_grob(
                  "Source: Berkeley Lab", 
                  hjust = 1,
                  x = .9,
                  face = "italic",
                  size = 9,
                  color = color.pal[1])
)

image.png


描述统计图


参考


Analytics X3: Tidy Tuesday US Solar | KPress R Blog(https://kpress.dev/blog/analytics-x3-tidytuesday-us-solar/)


相关文章
|
2月前
|
数据可视化 Python
Matplotlab可视化学习笔记(二):如何绘制柱状图
使用Matplotlib库在Python中绘制柱状图的教程,包括基本的柱状图绘制和多条柱状图的绘制方法。
32 1
Matplotlab可视化学习笔记(二):如何绘制柱状图
|
3月前
R语言基于表格文件的数据绘制具有多个系列的柱状图与直方图
【9月更文挑战第9天】在R语言中,利用`ggplot2`包可绘制多系列柱状图与直方图。首先读取数据文件`data.csv`,加载`ggplot2`包后,使用`ggplot`函数指定轴与填充颜色,并通过`geom_bar`或`geom_histogram`绘图。参数如`stat`, `position`, `alpha`等可根据需要调整,实现不同系列的图表展示。
|
7月前
|
数据可视化 数据挖掘 定位技术
R语言读取Excel表格数据并绘制多系列柱状图、条形图
R语言读取Excel表格数据并绘制多系列柱状图、条形图
175 1
|
6月前
|
数据可视化 开发者 Python
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)-1
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)
|
6月前
|
Python
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)-2
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)
|
7月前
|
NoSQL 数据库 Python
PyQt学习(二)-----图形的建立(柱状图,折线图,堆叠柱状图,饼图)
PyQt学习(二)-----图形的建立(柱状图,折线图,堆叠柱状图,饼图)
103 1
|
7月前
|
数据可视化 数据挖掘 Python
Matplotlib图表类型详解:折线图、柱状图与散点图
【4月更文挑战第17天】本文介绍了Python数据可视化库Matplotlib的三种主要图表类型:折线图、柱状图和散点图。折线图用于显示数据随时间或连续变量的变化趋势,适合多条曲线对比;柱状图适用于展示分类数据的数值大小和比较;散点图则用于揭示两个变量之间的关系和模式。通过示例代码展示了如何使用Matplotlib创建这些图表。
|
7月前
【SPSS】基础图形的绘制(条形图、折线图、饼图、箱图)详细操作过程(上)
【SPSS】基础图形的绘制(条形图、折线图、饼图、箱图)详细操作过程
650 0
|
7月前
【SPSS】基础图形的绘制(条形图、折线图、饼图、箱图)详细操作过程(下)
【SPSS】基础图形的绘制(条形图、折线图、饼图、箱图)详细操作过程
453 0