我想在R中创建图形(请参见图片Each horizontal column represents choice from two possibilities and number or proportion of such cases)。我尝试过李克特包,这是可能只在一列中使用一个类别变量,但在一列中需要两个(计数或比例)变量。我正在尝试的数据如下。每行是一列,植物1在左侧,植物2在右侧,总计是要显示的病例数,昆虫是所有人的总称。 您能推荐我合适的代码或软件包吗?
+--------+---------+--------+--------------+--------------+
| Insect | Plant1 | Plant2 | Total plant1 | Total plant2 |
+--------+---------+--------+--------------+--------------+
| SF | Maize | Cotton | 38 | 12 |
| SF | Cabbage | Cotton | 40 | 10 |
| SF | Cabbage | Maize | 42 | 8 |
+--------+---------+--------+--------------+--------------+
答案 0 :(得分:0)
您可以使用ggplot2
来做到这一点。
示例代码示例:
data <- tibble(
Plant1 = c('Maize', 'Cabbage', 'Cabbage'),
Plant2 = c('Cotton', 'Cotton', 'Maize'),
TotalPlant1 = c(38, 40, 42),
TotalPlant2 = c(12, 10, 8)
)
data %>%
mutate(row = row_number()) %>%
gather('x', 'Plant', c(Plant1, Plant2)) %>%
mutate(
Total = if_else(x == 'Plant1', TotalPlant1, -TotalPlant2),
lab_pos = if_else(x == 'Plant1', Total + max(Total) * .02, Total - max(Total) * .02),
hj = if_else(x == 'Plant1', 0, 1)
) %>%
select(row, Plant, Total, lab_pos, hj) %>%
ggplot(aes(
x = row,
y = Total
)) +
geom_col(fill = 'transparent', color = 'black') +
geom_text(aes(
y = lab_pos,
hjust = hj,
label = Plant
)) +
geom_hline(yintercept = 0) +
coord_flip() +
xlab('') +
ylab('') +
scale_y_continuous(
breaks = seq(-20, 40, 10),
labels = seq(-20, 40, 10) %>% abs(),
expand = expand_scale(mult = c(.2, .2))
) +
theme(
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line()
)