[英]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()
雖然有一些我無法理解的問題。
無論如何,可能需要更大比例的數據來參考您的問題:
首先讓我們了解一下圖表:我們正在查看每個國家和每個月的利潤(X 軸很擁擠)。 這意味着,我們看到了月份國家組合的多次出現。 原因可能是缺少年份信息或附加但缺少的變量,因此未知變量。 它很可能是您的數據分析過程中丟失的年份信息。 您可能必須在圖表中對此進行控制。
由於我們正在查看利潤,因此這些值可能確實是負數(需要完整數據,但您可以按升序排序或過濾值 < 0 進行檢查)
填充是指未知變量(因此某些國家可能在某些年份的某個月份確實是負數)
讓我們用您的剪輯模擬一些數據(包括年份)
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.