簡體   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