1

I'm trying to find out why I'm not able to change the color of my bars. Hope you can help, I'm kinda new to this.

ggplot2(data, aes(x = data$Crop, y = data$"2018"))+
    geom_bar(color="black", fill="red") +
    theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1)) +
    main="Production value per crop in 2018" +
    ylab("Production value in 2018")+
    xlab("Crop")+

Hope to hear from you.

T.R Bedijn
  • 11
  • 2
  • Please share a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). At least some data so we can run the code by ourselves. – Martin Gal May 28 '20 at 13:47
  • `ggplot(data, aes(x=Crop)) + geom_bar(color="black", fill="red")` should do the trick. What is your expected output and what does your actual output look like? – Martin Gal May 28 '20 at 13:53
  • You shouldn't use `$` inside `aes()`. It's better to switch to `aes(x = Crop, y = \`2018\`)` – Gregor Thomas May 28 '20 at 14:23
  • Eventually I managed to do it. – T.R Bedijn May 28 '20 at 21:29

2 Answers2

1

A little tweak of your code:

library(tidyverse)
data <- data.frame(Crop = c("East","West","North","South"),
                   Y2018 = c(1000,2000,3000,400),
                      stringsAsFactors = TRUE)

ggplot(data, aes(x = data$Crop, y = data$Y2018)) + 
  geom_col(color="black", fill="red") + 
  theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1)) + 
  labs(
    title = "Production value per crop in 2018",
    ylab = "Production value in 2018",
    xlab = "Crop"
  )

Hope it helps

Alexis
  • 2,104
  • 2
  • 19
  • 40
0

This is the code, which worked, though xlab gave an error referring to unexpected symbol

setwd("C:/Users/####/OneDrive/Documenten") # include the path to your data
data<-read_xlsx("datasetR.xlsx")
str(data) # get an overview of the data


library(ggplot2)
library(tidyverse)
dCrop <- data$Crop
d2018 <- data$"2018"

ggplot(data, aes(x = dCrop, y = d2018)) +
    geom_col(width=1, fill = "red") +
    theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1)) +
    ggtitle("Production value per crop in 2018")+
    ylab("Production value in 2018") +
    xlab("Crop")

enter image description here

T.R Bedijn
  • 11
  • 2