简体   繁体   English

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

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

[enter image description here][1] [在此处输入图片描述][1]

[1]: https://i.stack.imgur.com/IgLOC.png - this is what i want to draw [1]: https ://i.stack.imgur.com/IgLOC.png - 这就是我想要画的

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

Not a complete solution, but the essence.不是一个完整的解决方案,而是本质。

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 is basically used to create elegant data visualization using graphics. ggplot2 基本上用于使用图形创建优雅的数据可视化。

Here is example:这是示例:

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

here we get output of dataset_col1 (x axis) and dataset_col2(y-axis)这里我们得到dataset_col1 (x axis)dataset_col2(y-axis)输出

Before running this code, you need to download the package -> ggplot2 from the packages在运行此代码之前,您需要从包中下载包 -> ggplot2

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

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