按类别分解一个大的 ggplot; subset 没有显示错误但没有绘制数据
Breaking up a large ggplot by category; subset shows no errors but plots no data
我有一个非常大的数据集,它来自以下格式的电子表格:
df = data.frame(name = c('Ger1', 'Ger2', 'Ger3', 'Ger4', 'Ger5', 'Ger6'),
issued = c(UKS, USD, UKS, UKS, USD, USD),
mat = c(2024-01-31, 2023-01-31, 2026-10-22, 2022-07-22, 2029-01-31, 2025-06-07)
volume = c(0.476, 0.922, 0.580, 1.259, 0.932, 0.417)
目前,我使用以下代码在一个非常长的 ggplot 上绘制所有数据:
chart1<-ggplot(df)+geom_bar(stat="ID",aes(x=volume,y=name),fill="#1170aa")+
theme(title=element_text(size=12),panel.background = element_rect(fill='white',color='black'),legend.position='right')+
labs(title = "Total carriage by Volume on the day", x = "Volume", y = "Name")
现在,虽然这种方法工作了一段时间,但考虑到数据集的大小已经增长到这种方式不再可行。因此,我想根据“发布”列的内容绘制数据。
我首先想到的是这种类型的条件语句:
if (df$issued == "UKS"){
chart1<-ggplot(df)+geom_bar(stat="ID",aes(x=volume,y=name),fill="#1170aa")+
theme(title=element_text(size=12),panel.background = element_rect(fill='white',color='black'),legend.position='right')+
labs(title = "Total carriage by Volume on the day", x = "Volume", y = "Name")
}
不幸的是它没有用(尽管仔细检查我的逻辑并不是特别深思熟虑)
然后我尝试使用 subset() 函数,希望只允许绘制满足我要求的数据:
chart1<-ggplot(subset(df, 'issued' == "UKS"))+geom_bar(stat="ID",aes(x=volume,y=name),fill="#1170aa")+
theme(title=element_text(size=12),panel.background = element_rect(fill='white',color='black'),legend.position='right')+
labs(title = "Total carriage by Volume on the day", x = "Volume", y = "Name")
此特定代码没有显示任何错误,但生成的图表上没有任何数据。有人对我如何过滤和绘制这些数据有任何想法吗?
subset()
中的列名不需要引号 ""
。
ggplot(subset(df, issued == "UKS")) +
geom_bar(stat="identity", aes(x=volume,y=name),fill="#1170aa")+
theme(title=element_text(size=12),
panel.background = element_rect(fill='white',color='black'),
legend.position='right')+
labs(title = "Total carriage by Volume on the day", x = "Volume", y = "Name")
或者使用tidyverse
绘图方式:
library(tidyverse)
df %>% filter(issued == "UKS") %>%
ggplot() +
geom_bar(stat="identity", aes(x=volume,y=name),fill="#1170aa")+
theme(title=element_text(size=12),
panel.background = element_rect(fill='white',color='black'),
legend.position='right')+
labs(title = "Total carriage by Volume on the day", x = "Volume", y = "Name")
我有一个非常大的数据集,它来自以下格式的电子表格:
df = data.frame(name = c('Ger1', 'Ger2', 'Ger3', 'Ger4', 'Ger5', 'Ger6'),
issued = c(UKS, USD, UKS, UKS, USD, USD),
mat = c(2024-01-31, 2023-01-31, 2026-10-22, 2022-07-22, 2029-01-31, 2025-06-07)
volume = c(0.476, 0.922, 0.580, 1.259, 0.932, 0.417)
目前,我使用以下代码在一个非常长的 ggplot 上绘制所有数据:
chart1<-ggplot(df)+geom_bar(stat="ID",aes(x=volume,y=name),fill="#1170aa")+
theme(title=element_text(size=12),panel.background = element_rect(fill='white',color='black'),legend.position='right')+
labs(title = "Total carriage by Volume on the day", x = "Volume", y = "Name")
现在,虽然这种方法工作了一段时间,但考虑到数据集的大小已经增长到这种方式不再可行。因此,我想根据“发布”列的内容绘制数据。
我首先想到的是这种类型的条件语句:
if (df$issued == "UKS"){
chart1<-ggplot(df)+geom_bar(stat="ID",aes(x=volume,y=name),fill="#1170aa")+
theme(title=element_text(size=12),panel.background = element_rect(fill='white',color='black'),legend.position='right')+
labs(title = "Total carriage by Volume on the day", x = "Volume", y = "Name")
}
不幸的是它没有用(尽管仔细检查我的逻辑并不是特别深思熟虑)
然后我尝试使用 subset() 函数,希望只允许绘制满足我要求的数据:
chart1<-ggplot(subset(df, 'issued' == "UKS"))+geom_bar(stat="ID",aes(x=volume,y=name),fill="#1170aa")+
theme(title=element_text(size=12),panel.background = element_rect(fill='white',color='black'),legend.position='right')+
labs(title = "Total carriage by Volume on the day", x = "Volume", y = "Name")
此特定代码没有显示任何错误,但生成的图表上没有任何数据。有人对我如何过滤和绘制这些数据有任何想法吗?
subset()
中的列名不需要引号 ""
。
ggplot(subset(df, issued == "UKS")) +
geom_bar(stat="identity", aes(x=volume,y=name),fill="#1170aa")+
theme(title=element_text(size=12),
panel.background = element_rect(fill='white',color='black'),
legend.position='right')+
labs(title = "Total carriage by Volume on the day", x = "Volume", y = "Name")
或者使用tidyverse
绘图方式:
library(tidyverse)
df %>% filter(issued == "UKS") %>%
ggplot() +
geom_bar(stat="identity", aes(x=volume,y=name),fill="#1170aa")+
theme(title=element_text(size=12),
panel.background = element_rect(fill='white',color='black'),
legend.position='right')+
labs(title = "Total carriage by Volume on the day", x = "Volume", y = "Name")