0

I want to create graph in R (see image Each horizontal column represents choice from two possibilities and number or proportion of such cases). I tried likert package, here is possibility working only with one categorical variable in one column, but i need two (count or proportional) variable in one column. Data which I'm trying are below. Each row is one column, plant 1 is on left, plant 2 is on right, Total are number of cases for displaying, Insect is general title for all. Could you recommend me somebody appropriate code or package?

+--------+---------+--------+--------------+--------------+
| Insect | Plant1  | Plant2 | Total plant1 | Total plant2 |
+--------+---------+--------+--------------+--------------+
| SF     | Maize   | Cotton |           38 |           12 |
| SF     | Cabbage | Cotton |           40 |           10 |
| SF     | Cabbage | Maize  |           42 |            8 |
+--------+---------+--------+--------------+--------------+
  • 2
    Please add sample data using `dput(head(data,20))`. Avoid pasting links to graphs, insert an image instead. Please also add sample code and a description of what you expect and the exact problem. Otherwise, this is off-topic. – NelsonGon Jan 06 '19 at 14:06
  • So I have inserted the data and image directly, and I'll hope for a solution. – Roman Modlinger Jan 06 '19 at 15:59
  • 1
    SO is not a code-writing service. Please show your attempt and any errors/undesired results that occur. – Parfait Jan 06 '19 at 16:05
  • Welcome to SO! Could you make your problem reproducible by sharing a sample of your data and the code you're working on so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Jan 06 '19 at 16:28
  • As for your posted data, see [this question](https://stackoverflow.com/questions/52023709/what-can-r-do-about-a-messy-data-format/52023815#52023815). – Rui Barradas Jan 06 '19 at 22:22

1 Answers1

0

You can do it with ggplot2.

Example plot with code:

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()
  )

enter image description here

Paweł Chabros
  • 2,349
  • 1
  • 9
  • 12