我正在嘗試使用我的資料創建一個圓形條形圖,但我什至無法組織資料框來完成它。我有來自 3 個不同年份(名為 campana 的列)和來自一個省的 4 個地區(名為 zona 的列)的 121 個種子分析。我想制作一個類似于影像中的圖表,使用不同的區域(zonas)而不是字母 ABCD,并使用堆疊條顯示每年真菌的發病率(頻率)。每個真菌頻率都在一個帶有真菌名稱的列中,例如 A.padwickii、Microdochium、Bipolaris 等。我不指望你做所有的作業,只是幫助我以正確的方式組織資料的第一步.
tibble::tribble(
~muestra_n, ~campana, ~zona, ~inc_pan, ~percent_gran_m_pan, ~peso_pan, ~percent_perdida_peso, ~severidad, ~phoma, ~microdochium, ~a_padwickii, ~bipolaris, ~curvularia, ~fusarium, ~exserohilum,
16, 2, 2, 98, 14.6, 2.85, 9.90007401924501, 0.1, 0, 11, 0, 0, 0, 0, 0,
18, 2, 2, 97.1, 7.8, 1.34, 29.9991559044484, 0.44, 13, 0, 0, 0, 0, 0, 0,
19, 2, 2, 100, 14.3, 1.35, 0.0288073746879383, 0.66, 0, 16, 0, 0, 18, 0, 0,
20, 2, 1, 100, 9.6, 1.49, 1.64930877284015, 0.09, 0, 12, 1, 2, 44, 0, 0,
21, 2, 1, 100, 17.9, 3.04, 1.46085792877853, 0.56, 0, 9, 0, 0, 1, 0, 0,
22, 2, 1, 100, 37.4, 2.1, 5.60829881602581, 0.6, 41, 8, 0, 0, 1, 0, 1
)

uj5u.com熱心網友回復:
沒有足夠的資訊可以確定,但希望這可以通過一些提示使您朝著正確的方向前進。根據我的經驗,極坐標圖往往需要根據其適合的位置進行大量定制,而不是任何特定的標準。當你制作它們時,這會使它們變得更加復雜。
library(tidyverse)
YOUR_DATA %>%
# this step reshapes your data to a format more suited for ggplot2; each
# fungi appearance is given its own row
pivot_longer(phoma:last_col()) %>%
# this sets up the main plot
ggplot(aes(name, value))
geom_col()
# these add the lines and text for zonas. If you have many of them, it
# might make sense to figure these out as a separate data frame and feed
# these into lines like `geom_segment(data = SUMMARY) geom_text(data =
# SUMMARY)`
annotate("segment", x = c(0.5,2.5,4.5), xend = c(2.4,4.4,7), y = -10, yend = -10)
annotate("text", x = c(1.5,3.5,5.5), y = -20, label = LETTERS[1:3])
# This adds space in the middle, changing it from a pie chart to a donut
scale_y_continuous(limits = c(-100, NA))
coord_polar() # converts into polar coordinates
theme_void() # gets rid of grid lines and axis labels

uj5u.com熱心網友回復:
正如喬恩斯普林所說,極地情節可能很難以您描述的方式定制。它們通常也難以閱讀和解釋,但那是另一回事。由于這些原因,大多數繪圖系統都無法輕松創建高度定制的極坐標圖。
為了實作您想要的,我們需要做很多手動作業,包括:
- 將資料重塑為“長”格式以使用 ggplot。
- 自定義計算以正確旋轉每個真菌文本標簽。
- “區域”輔助 x 軸的自定義計算。
- 使用 ggplot 的摘要函式來確保文本標簽正確放置在每個條形堆疊的頂部。
以下代碼實作了所有這些。在這里,我在“campana”的第二個值下復制了資料,以驗證當條形堆疊時是否正確繪制。旁邊的評論:
# reshape the data to "long" format
df.long <- df %>% # YOUR DATA
select(zona, campana, phoma:exserohilum) %>%
pivot_longer(-c(zona, campana), names_to = 'fungus', values_to = 'freq') %>%
filter(freq > 0)
# duplicate the data and change campana so that we have some stacks to work with
df.long <- df.long %>%
bind_rows(mutate(df.long, campana = 3))
df.long.summary <- df.long %>%
group_by(combined_label = paste(zona, fungus, sep = '_'), fungus, campana, zona) %>% # a label that combines zona and fungus for the x-axis
summarize(freq = sum(freq)) %>% # totals
ungroup %>%
mutate(
x = as.numeric(factor(combined_label)), # the numerical position of each x-axis label
campana = factor(campana), # optional, but ggplot produces better colors this way
angle = (max(x) 0.45 - x) / max(x) * 2*pi * 180/pi 90, # calculate an angle for each text label
angle = angle %% 360, # constrain to [0-360]
flip = angle > 90 & angle < 270, # angles that fall in this range will need to be flipped for easier reading
angle = ifelse(flip, angle 180, angle), # flip the angle
hjust = ifelse(flip, 1.1, -0.1) # flip the hjust parameter to match
)
# create secondary x-axis parameters for "zona"
df.zona <- df.long.summary %>%
group_by(zona) %>%
summarize(
zona_x = min(x) - 0.4,
zona_xend = max(x) 0.4,
label_x = mean(x),
angle = 90 - (max(x) - zona_x) / max(x) * 2*pi * 180/pi 90
)
plot.fungi <- df.long.summary %>%
ggplot(data = ., aes(x = x, y = freq))
geom_col(position = 'stack', aes(fill = campana))
geom_text(aes(label = fungus, x = x, y = freq, angle = angle, hjust = hjust), stat = 'summary', fun = sum)
geom_segment(data = df.zona, aes(x = zona_x, xend = zona_xend), y = -10, yend = -10)
geom_text(data = df.zona, aes(label = zona, x = label_x, y = -12, angle = 0, vjust = sign(angle) * 2))
coord_polar(clip = 'off')
scale_y_continuous(limits = c(-100, NA))
theme_void()
print(plot.fungi)

創建相同資料的笛卡爾(非極坐標)圖會更簡單(在我看來,這也更容易閱讀):
# reshape the data to "long" format
df.long <- df %>%
select(zona, campana, phoma:exserohilum) %>%
pivot_longer(-c(zona, campana), names_to = 'fungus', values_to = 'freq') %>%
filter(freq > 0)
df.bar <- df.long %>%
mutate(
campana = factor(campana),
zona = paste0('Zona ', zona)
) %>%
ggplot(data = ., aes(x = fungus, y = freq, fill = campana))
geom_col(position = 'stack')
facet_grid(facets = ~zona, scales = 'free_x', space = 'free_x')
theme_minimal()
theme(
axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),
strip.background.x = element_rect(fill = '#cccccc', color = NA)
)
print(df.bar)

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/419512.html
標籤:
