0

I have a dataset like these

Region Greenspace Bluespace
North 30 5
South 50 10
West 15 2
East 10 1
data <- data.frame (  stringsAsFactors = FALSE,
  Region = c("North", "South", "West", "East"),
  Greenspace = c(30,50,15,10),
  Bluespace = c(5,10,2,1)
)

I want to create a histogram showing the regions in the x-axis and percentage in the yaxis. I used that code

data %>% pivot_longer(cols = Greenspace:Bluespace) %>% ggplot(aes(x = Region, y = value, fill = name)) + geom_col(position = 'dodge') + ggtitle("Contrast between 4 different region areas for Greenspace and Bluespace") + xlab("Region") + ylab("percentage")+ scale_fill_manual(values=c("blue", "dark green"))

Now the problem is instead of name i want space in the legend. I change fill into name but got error message could not find space.

But the problem is i don't want name, I want space

Has anyone an idea how to fix it. I appreciate every help :)

enter image description here

1 Answers1

0

That's what labs is for. You can assign names to the different aesthetics, which will then be shown as the title of this aesthetic:

library(tidyverse)
data %>% 
  pivot_longer(cols = Greenspace:Bluespace) %>% 
  ggplot(aes(x = Region, y = value, fill = name)) + 
  geom_col(position = 'dodge') + 
  ggtitle("Contrast between 4 different region areas for Greenspace and Bluespace") + 
  scale_fill_manual(values = c(Bluespace = "blue", 
                               Greenspace = "dark green")) +
  labs(x = "Region", y = "percentage", fill = "space")

Created on 2022-08-23 by the reprex package (v2.0.1)

I think this becomes clear when looking at the x and y aesthetic, which you labeled using the special functions xlab and ylab. If you put this within labs the axis titles change. As a side note, you can remove the title of an aesthetic by setting, for example, labs(fill = NULL) or labs(fill = "").

JBGruber
  • 11,727
  • 1
  • 23
  • 45