如何避免叠加面板图中具有相同值的2个x相加

huangapple go评论79阅读模式
英文:

How to avoid 2 x with the same values adding up in a stacked panel graph

问题

谢谢
我有一个后续问题。

这是信息

df1<-data.frame(
  PPE=c("手套", "手套", "口罩", "口罩", "靴子", "靴子", "全套", "全套", "镜片", 
        "镜片", "其他", "其他", "手套", "手套", "口罩", "口罩", "靴子", "靴子", "全套", 
        "全套", "镜片", "镜片", "其他", "其他", "手套", "手套", "口罩", "口罩", "靴子", 
        "靴子", "全套", "全套", "镜片", "镜片", "其他", "其他", "手套", "手套", "口罩",
        "口罩", "靴子", "靴子", "全套", "全套", "镜片", "镜片", "其他", "其他", "手套", 
        "手套", "口罩", "口罩", "靴子", "靴子", "全套", "全套", "镜片", "镜片", "其他", 
        "其他", "手套", "手套", "口罩", "口罩", "靴子", "靴子", "全套",
        "全套", "镜片", "镜片", "其他", "其他", "手套", "手套", "口罩", "口罩", "靴子", 
        "靴子", "全套", "全套", "镜片", "镜片", "其他", "其他", "手套", "手套", "口罩", 
        "口罩", "靴子", "靴子", "全套", "全套", "镜片", "镜片", "其他", 
        "其他")
  ,Choice =c("否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是",
             "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", 
             "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", 
             "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是", "否", "是") 
  ,Farmtype =c("谷物", "谷物", "谷物", "谷物", "谷物", "谷物", "谷物", "谷物", "谷物", "谷物", "谷物", "谷物",
               "牧草", "牧草", "牧草", "牧草", "牧草", "牧草", "牧草", "牧草", "牧草", "牧草", "牧草", "牧草", 
               "水果", "水果", "水果", "水果", "水果", "水果", "水果", "水果", "水果", "

<details>
<summary>英文:</summary>

Thank you
I have a follow up question. 

This is the information

df1<-data.frame(
PPE=c("Gloves", "Gloves", "Mask", "Mask", "Boots", "Boots", "Overall", "Overall", "Lense",
"Lense", "Others", "Others", "Gloves", "Gloves", "Mask", "Mask", "Boots", "Boots", "Overall",
"Overall", "Lense", "Lense", "Others", "Others", "Gloves", "Gloves", "Mask", "Mask", "Boots",
"Boots", "Overall", "Overall", "Lense", "Lense", "Others", "Others", "Gloves", "Gloves", "Mask",
"Mask", "Boots", "Boots", "Overall", "Overall", "Lense", "Lense", "Others", "Others", "Gloves",
"Gloves", "Mask", "Mask", "Boots", "Boots", "Overall", "Overall", "Lense", "Lense", "Others",
"Others", "Gloves", "Gloves", "Mask", "Mask", "Boots", "Boots", "Overall", "Overall", "Lense",
"Lense", "Others", "Others", "Gloves", "Gloves", "Mask", "Mask", "Boots", "Boots", "Overall",
"Overall", "Lense", "Lense", "Others", "Others", "Gloves", "Gloves", "Mask", "Mask", "Boots",
"Boots", "Overall", "Overall", "Lense", "Lense", "Others", "Others")
,Choice =c("No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes",
"No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes",
"No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No",
"Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes",
"No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes")
,Farmtype =c("Cereal", "Cereal", "Cereal", "Cereal", "Cereal", "Cereal", "Cereal", "Cereal", "Cereal", "Cereal", "Cereal", "Cereal",
"Forage", "Forage", "Forage", "Forage", "Forage", "Forage", "Forage", "Forage", "Forage", "Forage", "Forage", "Forage",
"Fruit", "Fruit", "Fruit", "Fruit", "Fruit", "Fruit", "Fruit", "Fruit", "Fruit", "Fruit", "Fruit", "Fruit", "Horticulture",
"Horticulture", "Horticulture", "Horticulture", "Horticulture", "Horticulture", "Horticulture", "Horticulture", "Horticulture",
"Horticulture", "Horticulture", "Horticulture", "Industrial", "Industrial", "Industrial", "Industrial", "Industrial", "Industrial",
"Industrial", "Industrial", "Industrial", "Industrial", "Industrial", "Industrial", "legumes", "legumes", "legumes", "legumes",
"legumes", "legumes", "legumes", "legumes", "legumes", "legumes", "legumes", "legumes", "Root.farms", "Root.farms", "Root.farms",
"Root.farms", "Root.farms", "Root.farms", "Root.farms", "Root.farms", "Root.farms", "Root.farms", "Root.farms", "Root.farms",
"Mixed", "Mixed", "Mixed", "Mixed", "Mixed", "Mixed", "Mixed", "Mixed", "Mixed", "Mixed", "Mixed", "Mixed")
,value=c(80.26,19.74,64.87,35.13,69.32,30.68,94.05,5.95,92.83,7.17,83.43,16.57,73.98,26.02,50.81,49.19,56.91,43.09,94.31,5.69,86.99,13.01,81.3,18.7,72.33,27.67,49.4,50.6,66.86,33.14,91.27,8.73,86.33,13.67,89.78,10.22,76.58,23.42,57.64,42.36,56.72,43.28,91.45,8.55,88.9,11.1,84.93,15.07,74.29,25.71,55.67,44.33,52.38,47.62,90.48,9.52,89.7,10.3,86.93,13.07,77.66,22.34,63.83,36.17,67.55,32.45,96.28,3.72,91.49,8.51,90.43,9.57,78.32,21.68,72.51,27.49,59.04,40.96,93.66,6.34,95.51,4.49,82.57,17.43,81.88,18.13,55.63,44.38,41.25,58.75,96.88,3.13,83.75,16.25,83.75,16.25)
)
summary(df1)
head(df1)
view(df1)
df2 <-
df1 |>
arrange(PPE, Farmtype, desc(Choice)) |>
mutate(order_val = ifelse(Choice == "Yes", value, NA_real_)) |>
fill(order_val)

#data frame for labels

labs <-
df2 |>
group_by(order_val) |>
filter(row_number() == 1)

Create the grouped stacked graph

I've simplified your version using geom_col rather than geom_bar

ggplot(df2, aes(x = fct_reorder(factor(order_val), order_val),
y = value,
fill = Choice)) +
geom_col() +
facet_grid(Farmtype ~ ., switch = "x", scales = "free_y") +
scale_x_discrete(breaks = labs$order_val,
labels = labs$PPE)+
labs(x = "PPE",
y = "Value",
fill = "Choice") +
ggtitle("Grouped and Stacked Graph") +

theme_minimal()

theme_classic()+
ggplot2::coord_flip()


The graph is [![enter image description here](https://i.stack.imgur.com/7yhEL.png)](https://i.stack.imgur.com/7yhEL.png)
I realized that the &quot;Others&quot; and &quot;Lense&quot; of df1$PPE have the same value for the Mixed farm as shown below.
[![enter image description here](https://i.stack.imgur.com/t5t6M.png)](https://i.stack.imgur.com/t5t6M.png)
This I think makes the graph that way. How do I make sure Lense and Others appear with their separate stacked bar in the graph?
Also, How do I make the values on the y axis appear in percentages like 0%, 50% and 100%? 
I tried to get all variables to appear on the graph
</details>
# 答案1
**得分**: 0
以下是翻译好的部分:
"Instead of mapping values on a discrete scale and afterwards assigning labels you could create a helper order column as the interaction of `Farmtype` and `PPE` after arranging your data in the desired order. Then use e.g. `gsub` to remove the `Farmtype` part from the axis label:
```R
library(tidyverse)
df1 <- df1 %>%
group_by(Farmtype, PPE) %>%
mutate(order_value = value[Choice == "No"]) %>%
ungroup() %>%
arrange(Farmtype, desc(order_value)) %>%
mutate(order = fct_inorder(paste(Farmtype, PPE, sep = ".")))
ggplot(df1, aes(
y = order,
x = value,
fill = Choice
)) +
geom_col() +
facet_grid(Farmtype ~ ., switch = "x", scales = "free_y") +
scale_y_discrete(
labels = ~ gsub("^.*\\.", "", .x)
) +
labs(
x = "PPE",
y = "Value",
fill = "Choice"
) +
ggtitle("Grouped and Stacked Graph") +
theme_classic()

如何避免叠加面板图中具有相同值的2个x相加"

英文:

Instead of mapping values on a discrete scale and afterwards assigning labels you could create a helper order column as the interaction of Farmtype and PPE after arranging your data in the desired order. Then use e.g. gsub to remove the Farmtype part from the axis label:

library(tidyverse)
df1 &lt;- df1 |&gt;
group_by(Farmtype, PPE) |&gt; 
mutate(order_value = value[Choice == &quot;No&quot;]) |&gt; 
ungroup() |&gt; 
arrange(Farmtype, desc(order_value)) |&gt; 
mutate(order = fct_inorder(paste(Farmtype, PPE, sep = &quot;.&quot;)))
ggplot(df1, aes(
y = order,
x = value,
fill = Choice
)) +
geom_col() +
facet_grid(Farmtype ~ ., switch = &quot;x&quot;, scales = &quot;free_y&quot;) +
scale_y_discrete(
labels = ~ gsub(&quot;^.*\\.&quot;, &quot;&quot;, .x)
) +
labs(
x = &quot;PPE&quot;,
y = &quot;Value&quot;,
fill = &quot;Choice&quot;
) +
ggtitle(&quot;Grouped and Stacked Graph&quot;) +
theme_classic()

如何避免叠加面板图中具有相同值的2个x相加

huangapple
  • 本文由 发表于 2023年5月25日 02:17:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定