簡體   English   中英

繪制和理解 face_wrap 和多變量圖 - ggplot

[英]Ploting and understanding face_wrap and multiplevariable graphs - ggplot

我是圖形和 r 的新手,我得到了分析數據集的練習。 我願意從中做一個圖表(facet_wrap?),其中我顯示了來自不同國家/地區的每月利潤分布。 我有一個包含 19 個國家、12 個月和利潤值的子集:類似於(示例):

Country   Month  Profit
Brazil    Jan     50
Brazil    fev     80
Brazil    mar     15
Austria   Jan     35
Austria   fev     80
Austria   mar     47
France    Jan     21
France    fev     66
France    mar     15
[...]
Germany   Dez     40 

我已經對圖表進行了一些嘗試,但我仍然在努力理解它是如何工作的。 到目前為止,我有:

test <- ggplot(sub, aes(x=Month, y=Profit, fill= Profit))+
geom_bar(stat='identity')+
facet_wrap(~Country) +
scale_fill_gradient(low = "red", high = "green", name = "Profit grade", labels = comma) +
scale_y_continuous(labels = function(x) format(x, scientific = FALSE))+
theme_bw()

這就像: 在此處輸入圖像描述

雖然有一些我無法理解的問題。

  1. 為什么不是所有的條都從同一行開始? 他們不是直的,這正常嗎? 或者為什么 X 軸會波動? (檢查法國以獲得清晰的圖片)
  2. 如何正確填寫? 在我的圖表上,當我使用 function 填充時,陰影出現在整個條形中,而不是在不同的 par 之間(這更有意義)。 你能解釋一下這個填充物(來自照片)可能是什么意思嗎?
  3. 是否有另一種/更好的方式來呈現此類信息? 我願意嘗試折線圖,比如電視上關於經濟學的那些山圖,但我不知道怎么做。 我嘗試了一些點,但對我沒有意義(或者我做錯了哈哈)

無論如何,可能需要更大比例的數據來參考您的問題:

首先讓我們了解一下圖表:我們正在查看每個國家和每個月的利潤(X 軸很擁擠)。 這意味着,我們看到了月份國家組合的多次出現。 原因可能是缺少年份信息或附加但缺少的變量,因此未知變量。 它很可能是您的數據分析過程中丟失的年份信息。 您可能必須在圖表中對此進行控制。

  1. 由於我們正在查看利潤,因此這些值可能確實是負數(需要完整數據,但您可以按升序排序或過濾值 < 0 進行檢查)

  2. 填充是指未知變量(因此某些國家可能在某些年份的某個月份確實是負數)

  3. 讓我們用您的剪輯模擬一些數據(包括年份)

n

library(tidyverse) # you could call only ggplot2 and dplyr
# read data from plain text as data.table/data.frame
dt <- data.table::fread('Year  Country   Month  Profit
2012   Brazil    Jan     50
2012   Brazil   fev     80
2012   Brazil    mar     15
2012   Austria   Jan     35
2012   Austria   fev     80
2012   Austria   mar     47
2012   France    Jan     21
2012   France    fev     66
2012   France    mar     15
2012   Germany   Dez     40
2013   Brazil    Jan     50
2013   Brazil   fev     80
2013   Brazil    mar     15
2013   Austria   Jan     35
2013   Austria   fev     80
2013   Austria   mar     47
2013   France    Jan     21
2013   France    fev     66
2013   France    mar     15
2013   Germany   Dez     40')

# the months have to be manipulated to make text recognition work - you can look up the info here and it seems you have portugues or brazilian data
# http://metodologia.lilacs.bvsalud.org/docs/pt/tabela-abreviatura-meses.htm
dtc <- dt %>%
    dplyr::mutate(Month = stringr::str_to_lower(Month), # text to small case - less substitutions
                  # correct months for text recognition (manipulation to lower case is performed in prior call and overwritten with new data
                  Month = dplyr::case_when(Month == 'fev' ~ 'feb',
                                           Month == 'dez' ~ 'dec',
                                           TRUE ~ Month),
              # use tidyverse recognition for dates - first of month in this case but the info helps to organize data as timeline
                  newdate = lubridate::ym(paste(Year, Month))) 

# a very very simple point chart that you can convert to line chat 
dtc %>%
    ggplot2::ggplot(aes(newdate, Profit)) +
    ggplot2::geom_point() +
    # ggplot2::geom_line() +
    ggplot2::facet_wrap(~Country) +
    ggplot2::theme(axis.text.x = element_text(angle = 90))

在此處輸入圖像描述

# or the option of a flipped column chart
dtc %>%
    ggplot2::ggplot(aes(newdate, Profit)) +
    ggplot2::geom_col() +
    ggplot2::facet_wrap(~Country) +
    ggplot2::coord_flip()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM