0

I have a dataset looking at the percentage of excitatory versus inhibitory cells across different parts of the brain. I want to create a plot where each part of the brain is on the x axis and the y axis is 100%. I want each bar of the brain area to be coloured the percentage that it is inhibitory and excitatory. Google drive version of graph I want

So far using the below code the plot is not what I want because it is just colouring the % of inhibitory as a gradient graph I don't want:

dataframe <- data.frame(area = c("area1", 'area2', "area3"), total_cells = c('30303', '57464', '28484'), percent_excitatory = c(10, 45, 60), percent_inhibitory = c(90, 55, 40))


ggplot(dataframe, aes(x=area, y=total_cells, fill=percent_inhibitory)) + 
  geom_bar(position = 'fill', stat = 'identity') +
  scale_y_continuous(labels = scales::percent_format()) +
  coord_flip()
LM1995
  • 1
  • 1
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Aug 14 '23 at 15:31
  • I have included some more info - is this what you mean? – LM1995 Aug 14 '23 at 15:38
  • It would be better to include sample data in the question itself so can can copy/paste the data and code into R to run and test it. There are examples in the link I provided. – MrFlick Aug 14 '23 at 15:42
  • Okay np - done that now – LM1995 Aug 14 '23 at 15:49
  • Running the sample data and code you provided I get the error "Error: Discrete value supplied to continuous scale". Do you also get that error? Is that the problem? – MrFlick Aug 14 '23 at 15:52
  • no sorry try this: ggplot(dataframe, aes(x=area, y=total_cells, fill = percent_inhibitory)) + geom_bar(stat = "Identity") – LM1995 Aug 14 '23 at 15:55
  • Your sample data has total cells as character values but you seem to want them as numeric. The problem is your data is in the wrong shape for the plot you want. Here's an example using `tidyr` to reshape the data. Try `dataframe %>% tidyr::pivot_longer(starts_with("percent"), names_pattern="percent_(.*)", names_to="category") %>%ggplot(aes(area, value, fill=category)) + geom_col(position = "fill")` – MrFlick Aug 14 '23 at 15:57

0 Answers0