-1

enter image description hereI am having a problem with my ggplot that i cannot insert a legend. I just want to show the total number of facilities per region (manually).

Here is my code: Note: my csv file has 17,333 IDs, I was thinking maybe that is why but I'm not really sure so.

library(ggplot2)
library(dplyr)
library(ggthemes)
library(tidyverse)


doh = read.csv("doh.csv")



doh %>%
  ggplot(aes( y = region, color = region)) +
  geom_bar(position = "identity", size = 0.7, alpha = 0.8, fill = "#28d1eb", colour="black") +
  
  labs(title = "Total Number of COVID-19 Facilities per Region",
       x = "Count",
       y = "Region") +
  theme_minimal() +
  
  theme(plot.title = element_text(lineheight=6, face="bold", color="black",size=15)) 

I have tried inserting a legend in my code but it isn't working and I'm not sure where I went wrong.

code:

library(ggplot2)
library(dplyr)
library(ggthemes)
library(tidyverse)


doh = read.csv("doh.csv")



doh %>%
  ggplot(aes( y = region, fill = region, color = region)) +
  geom_bar(position = "identity", size = 1.0, alpha = 0.8, fill = "#28d1eb", colour="black") +
  
  labs(title = "Total Number of COVID-19 Facilities per Region") +
  theme_minimal() +
  
  theme(plot.title = element_text(lineheight=6, face="bold", color="black",size=15)) +
  barplot(data, 
          col = c("#f2b50c", "#d96fe3")) +
  legend("topright",
         legend = c("REGION XIII (CARAGA)"))


PS: I only included the "REGION XIII (CARAGA)" because I just want to see if its working but its not.

enter image description here

Thanks in adv!

  • 3
    Welcome to SO. You can share a small part of your data by using `dput(head(doh))`. That way people can look at the data and potentially see what might be causing the problem. The other thing that would help in this case is knowing your error message. You can add that to your post as well. Use the Edit button to add the output from `dput` and the error message. – John Polo Nov 19 '22 at 01:48
  • The problem is you’re specifying `fill = "#28d1eb"` inside `geom_bar()`, which is overriding the fill aesthetic mapped to `region`. – zephryl Nov 19 '22 at 02:08
  • 1
    [Duplicate Question](https://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot) – mccurcio Nov 19 '22 at 02:25
  • Does this answer your question? [Add legend to ggplot2 line plot](https://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot) – jrcalabrese Nov 20 '22 at 16:40

1 Answers1

0

Here is a way. Count the regions before piping to ggplot.
The example below uses the data set diamonds, substitute region for clarity and the code should work.

suppressPackageStartupMessages({
  library(ggplot2)
  library(dplyr)
})

data(diamonds)

diamonds %>%
  select(clarity) %>%
  count(clarity, name = "Count") %>%
  ggplot(aes(x = clarity, y = Count, fill = clarity)) +
  geom_col(alpha = 0.8, fill = "#28d1eb", colour = "black") +
  geom_text(aes(label = Count), hjust = -0.1) +
  coord_flip() +
  labs(title = "Total Number of COVID-19 Facilities per Region") +
  theme_minimal(base_size = 15) +
  theme(plot.title = element_text(lineheight = 6, face = "bold", color="black"))

Created on 2022-11-21 with reprex v2.0.2

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66