繁体   English   中英

如何通过“ggplot2”创建这种类型的图

[英]how to create this type of plot by 'ggplot2'

[在此处输入图片描述][1]

[1]: https ://i.stack.imgur.com/IgLOC.png - 这就是我想要画的

[2]: https ://i.stack.imgur.com/S2YfL.png - 这是该图的数据

不是一个完整的解决方案,而是本质。

library(tidyverse)
library(ggplot2)

df <- tibble(
  type = c("barley",
           "maize",
           "sunlower oil",
           "wheat"),
  ukraine = c(0.974, 0.1643, 0.4221, 0.0891),
  russia = c(0.095, 0.0160, 0.2140, 0.1420)
)

# A tibble: 4 x 3
  type         ukraine russia
  <chr>          <dbl>  <dbl>
1 barley        0.974   0.095
2 maize         0.164   0.016
3 sunlower oil  0.422   0.214
4 wheat         0.0891  0.142

df %>%  
  gather(-type, key = "country", value = "value") %>%  
  ggplot() + 
  aes(x = type, y = value, fill = country) + 
  geom_col() + 
  coord_flip() 

在此处输入图像描述

ggplot2 基本上用于使用图形创建优雅的数据可视化。

这是示例:

library(ggplots2) 
ggplot2(dataset, aes(x='dataset_col1', y='dataset_col2')

这里我们得到dataset_col1 (x axis)dataset_col2(y-axis)输出

在运行此代码之前,您需要从包中下载包 -> ggplot2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM