我希望這次我以正確的方式提出了我的問題!如果不告訴我!我想撰寫一個與此類似的分組條形圖(我剛剛在繪畫中創建): 在此處輸入影像描述 我創建為翻轉兩者實際上是否翻轉都無關緊要。因此,與此類似的圖也將非常有用: Grouped barchart in r with 4 variables
變數快樂和生活滿意都是從 0 到 10 的標度值。作業時間是一個分組值,包含 43 、37-42、33-36、27-32 和 <27。
我的資料集看起來如何的一個非常相似的示例(我只是更改了值和順序,我還有更多觀察結果):
| 作業時間 | 快樂的 | 生活穩定 | 國家 |
|---|---|---|---|
| 37-42 | 7 | 9 | 丹麥 |
| <27 | 8 | 8 | 東南 |
| 43 | 7 | 8 | 丹麥 |
| 33-36 | 6 | 6 | 東南 |
| 37-42 | 7 | 5 | 不 |
| <27 | 4 | 7 | 不 |
我試圖找到類似的示例,并基于該示例嘗試以以下方式對條形圖進行編碼,但它不起作用:
df2 <- datafilteredwomen %>%
pivot_longer(cols = c("happy", "stflife"), names_to = "var", values_to = "Percentage")
ggplot(df2)
geom_bar(aes(x = Percentage, y = workinghours, fill = var ), stat = "identity", position = "dodge") theme_minimal()
它給出了這個不正確的情節/我想要的: 在此處輸入影像描述
第二次嘗試:
forplot = datafilteredwomen %>% group_by(workinghours, happy, stflife) %>% summarise(count = n()) %>% mutate(proportion = count/sum(count))
ggplot(forplot, aes(workinghours, proportion, fill = as.factor(happy)))
geom_bar(position = "dodge", stat = "identity", color = "black")
給出了這個情節: 在此處輸入影像描述
第三次嘗試 - 使用了 ggplot2 builder 插件:
library(dplyr)
library(ggplot2)
datafilteredwomen %>%
filter(!is.na(workinghours)) %>%
ggplot()
aes(x = workinghours, group = happy, weight = happy)
geom_bar(position = "dodge",
fill = "#112446")
theme_classic() scale_y_continuous(labels = scales::percent)
給出了這個情節: 在此處輸入影像描述
但是我的嘗試都不是我想要的..真的希望有人可以幫助我,如果可能的話!
uj5u.com熱心網友回復:
在與 OP 交談后,我找到了他的資料源并提出了這個解決方案。抱歉,如果它有點混亂,我只使用 R 6 個月。為了便于重現,我預先選擇了原始資料集中使用的變數。
data <- structure(list(wkhtot = c(40, 8, 50, 40, 40, 50, 39, 48, 45,
16, 45, 45, 52, 45, 50, 37, 50, 7, 37, 36), happy = c(7, 8, 10,
10, 7, 7, 7, 6, 8, 10, 8, 10, 9, 6, 9, 9, 8, 8, 9, 7), stflife = c(8,
8, 10, 10, 7, 7, 8, 6, 8, 10, 9, 10, 9, 5, 9, 9, 8, 8, 7, 7)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
這是所需的軟體包。
require(dplyr)
require(ggplot2)
require(tidyverse)
在這里,我操縱了資料并評論了我的推理。
data <- data %>%
select(wkhtot, happy, stflife) %>% #Select the wanted variables
rename(Happy = happy) %>% #Rename for graphical sake
rename("Life Satisfied" = stflife) %>%
na.omit() %>% # remove NA values
group_by(WorkingHours = cut(wkhtot, c(-Inf, 27, 32,36,42,Inf))) %>% #Create the ranges
select(WorkingHours, Happy, "Life Satisfied") %>% #Select the variables again
pivot_longer(cols = c(`Happy`, `Life Satisfied`), names_to = "Criterion", values_to = "score") %>% # pivot the df longer for plotting
group_by(WorkingHours, Criterion)
data$Criterion <- as.factor(data$Criterion) #Make criterion a factor for graphical reasons
多一點資料準備
# Creating the percentage
data.plot <- data %>%
group_by(WorkingHours, Criterion) %>%
summarise_all(sum) %>% # get the sums for score by working hours and criterion
group_by(WorkingHours) %>%
mutate(tot = sum(score)) %>%
mutate(freq =round(score/tot *100, digits = 2)) # get percentage
創建情節。
# Plotting
ggplot(data.plot, aes(x = WorkingHours, y = freq, fill = Criterion))
geom_col(position = "dodge")
geom_text(aes(label = freq),
position = position_dodge(width = 0.9),
vjust = 1)
xlab("Working Hours")
ylab("Percentage")
請讓我知道是否有更簡潔或更簡單的方法!
乙
資料源:https ://www.europeansocialsurvey.org/downloadwizard/?fbclid=IwAR2aVr3kuqOoy4mqa978yEM1sPEzOaghzCrLCHcsc5gmYkdAyYvGPJMdRp4
uj5u.com熱心網友回復:
以這個示例資料框df為例:
df <- structure(list(Working.hours = c("37-42", "37-42", "<27", "<27",
"43 ", "43 ", "33-36", "33-36", "37-42", "37-42", "<27", "<27"
), country = c("DK", "DK", "SE", "SE", "DK", "DK", "SE", "SE",
"NO", "NO", "NO", "NO"), criterion = c("happy", "lifesatisfied",
"happy", "lifesatisfied", "happy", "lifesatisfied", "happy",
"lifesatisfied", "happy", "lifesatisfied", "happy", "lifesatisfied"
), score = c(7L, 9L, 8L, 8L, 7L, 8L, 6L, 6L, 7L, 5L, 4L, 7L)), row.names = c(NA,
-12L), class = c("tbl_df", "tbl", "data.frame"))
你可以這樣進行:
library(dplyr)
library(ggplot2)
df <-
df %>%
pivot_longer(cols = c(happy, lifesatisfied),
names_to = 'criterion',
values_to = 'score'
)
df %>%
ggplot(aes(x = Working.hours,
y = score,
fill = criterion))
geom_col(position = 'dodge')
coord_flip()
有關選擇顏色?scale_fill_manual,請參閱有關格式化圖例等的有關 stackoverflow 相關問題的眾多現有答案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/466731.html
上一篇:ggstattsplot:為什么ggwithinstats不能處理分類資料
下一篇:基于具有相同變數的兩組創建圖
