将 R 中的两个密度图合并为一个图
Combine two density plots in R into one plot
我想将我在 Rstudio 中创建的两个单独的密度图组合成一个同时显示两者的图。
我使用以下代码创建了两个单独的密度图:
1 # create density plot for total sales
dens_plot_sales <- final_data %>%
drop_na(tot_sales, firm_size) %>%
ggplot()+
geom_density(aes(x = tot_sales, colour = firm_size)) +
labs(title = "Density plot of total sales across firm size levels",
x = "Total sales", y = "Density", col= "Firm size") +
theme_classic()
2 # create density plot for total costs
dens_plot_costs <- final_data %>%
drop_na(tot_costs, firm_size) %>%
ggplot()+
geom_density(aes(x = tot_costs, colour = firm_size)) +
labs(title = "Density plot of total costs across firm size levels",
x = "Total costs", y = "Density", col= "Firm size") +
theme_classic()
如何将地块 1 (dens_plot_sales) and plot 2 (dens_plot_costs) 合二为一?
(参考附图)
谢谢!
# create density plot for total sales and costs
dens_plot_sales <- final_data %>%
drop_na(tot_sales, firm_size) %>%
ggplot()+
geom_density(aes(x = tot_sales, colour = firm_size)) +
geom_density(aes(x = tot_costs, colour = firm_size)) + # It's that simple
labs(title = "Density plot of total sales and costs across firm size levels",
x = "Total sales/costs ($)", y = "Density", col= "Firm size") +
theme_classic()
如果不知道 final_data
是什么,我无法对其进行全面测试,但这应该有效
我想将我在 Rstudio 中创建的两个单独的密度图组合成一个同时显示两者的图。
我使用以下代码创建了两个单独的密度图:
1 # create density plot for total sales
dens_plot_sales <- final_data %>%
drop_na(tot_sales, firm_size) %>%
ggplot()+
geom_density(aes(x = tot_sales, colour = firm_size)) +
labs(title = "Density plot of total sales across firm size levels",
x = "Total sales", y = "Density", col= "Firm size") +
theme_classic()
2 # create density plot for total costs
dens_plot_costs <- final_data %>%
drop_na(tot_costs, firm_size) %>%
ggplot()+
geom_density(aes(x = tot_costs, colour = firm_size)) +
labs(title = "Density plot of total costs across firm size levels",
x = "Total costs", y = "Density", col= "Firm size") +
theme_classic()
如何将地块 1 (dens_plot_sales) and plot 2 (dens_plot_costs) 合二为一? (参考附图)
谢谢!
# create density plot for total sales and costs
dens_plot_sales <- final_data %>%
drop_na(tot_sales, firm_size) %>%
ggplot()+
geom_density(aes(x = tot_sales, colour = firm_size)) +
geom_density(aes(x = tot_costs, colour = firm_size)) + # It's that simple
labs(title = "Density plot of total sales and costs across firm size levels",
x = "Total sales/costs ($)", y = "Density", col= "Firm size") +
theme_classic()
如果不知道 final_data
是什么,我无法对其进行全面测试,但这应该有效