Kaja
Kaja

Reputation: 3057

setting y-axis in ggplot2

How can I change Y-Axis in ggplot2. I am using this code but I get this error:

k <- read.table(text="      name1      Ereigniss   distance
   kamel      kamel       1,251
   kamel      Dumper      2,750
   kamel      Graben      2,702
   kamel      Traktor     2.716
   Dumper     Kamel       2,750
   Dumper     Dumper      2,050
   Dumper     Graben      2,703
   Dumper     Traktor     2,570
   Graben     Kamel       2,702
   Graben     Dumper      2,703
   Graben     Graben      0,701
   Graben     Traktor     2,840
   Traktor    kamel       2,716
   Traktor    Dumper      2,570
   Traktor    Graben      2,840
   Traktor    Traktor     1,026
 ", header=T)
ggplot(k, aes(factor(name1), distance, fill = Ereigniss)) + 
  geom_bar(stat="identity", position = "dodge") + 
  scale_fill_brewer(palette = "Set1")+
  labs(x="Ereignisse",y="Distanz")+
  ylim(c(0,10))

then Iam getting this error:

Discrete value supplied to continuous scale

Upvotes: 0

Views: 255

Answers (1)

Koundy
Koundy

Reputation: 5503

There are a couple of issues 1) You have remove the comma in your "distance" variable in the numbers.

2) Why are you restricting the y-axis between 0 and 10 ?? all your values are in thousands. I made these changes in your code and got this nice plot.

# remove commas (and correct possible typo on 2.716)
k$distance <- as.numeric(gsub("[,|.]", "", k$distance))

library(ggplot2)
ggplot(k, aes(factor(name1), distance, fill = Ereigniss)) + 
      geom_bar(stat="identity", position = "dodge") + 
      scale_fill_brewer(palette = "Set1")+
      labs(x="Ereignisse",y="Distanz")

i

Upvotes: 1

Related Questions