Ananya Vidyanathan
Ananya Vidyanathan

Reputation: 51

Unable to change text labels in legend box and showing up different color in legend using ggplot

My dataframe is

 steps.1 interval.1 steps.2 interval.2 steps.3 interval.3
1         0          0       0          0       0          0
2         0          5       0          5       0          5
3         0         10       0         10       0         10
4         0         15       0         15       0         15
5         0         20       0         20       0         20
6         0         25       0         25       0         25
7         0         30       0         30       0         30
8         0         35       0         35       0         35
9         0         40       0         40       0         40
10        0         45       0         45       0         45.................
.
.
.
.

I am trying to plot a graph between each of step.1 vs interval.1 using enter image description hereggplot

My code

g<-ggplot(temp,aes(x=interval.1,y=steps.1))+scale_x_continuous(name="intervals",breaks = seq(1000,1500,50),limits = c(1000,1500))

g <- g + geom_jitter(aes(x=interval.1,y=steps.1,color="dark red")) + geom_jitter(aes(x=interval.2,y=steps.2,color="green"))+ geom_jitter(aes(x=interval.3,y=steps.3,color="orange"))

+ geom_jitter(aes(x=interval.4,y=steps.4,color="violet"))+ geom_jitter(aes(x=interval.5,y=steps.5),color="blue") + geom_jitter(aes(x=interval.6,y=steps.6,color="pink"))

But the legend box appears with random colors, ie, if I give color green it is plotting for different color and I am unable to change the name of each text labels in legend box?

Upvotes: 1

Views: 296

Answers (2)

Arun kumar mahesh
Arun kumar mahesh

Reputation: 2359

Please try this code, instead of giving color, keep variables names as is in color (eg: color = interval.1)

data

steps.1 interval.1  steps.2 interval.2  steps.3 interval.3
1   1000    0   1000    0   1000
2   1100    5   1100    5   1100
3   1200    10  1200    10  1200
4   1300    15  1300    15  1300
5   1400    20  1400    20  1400
6   1401    25  1401    25  1401
7   1402    30  1402    30  1402
8   1403    35  1403    35  1403
9   1404    40  1404    40  1404
10  1405    45  1405    45  1405

code

g<-ggplot(temp,aes(x=interval.1,y=steps.1))+scale_x_continuous(name="intervals",breaks = seq(1000,1500,50),limits = c(1000,1500))

g <- g + geom_jitter(aes(x=interval.1,y=steps.1,color="interval.1")) + geom_jitter(aes(x=interval.2,y=steps.2,color="interval.2"))+ geom_jitter(aes(x=interval.3,y=steps.3,color="interval.3"))

#output

enter image description here

Upvotes: 1

wisamb
wisamb

Reputation: 502

I couldn't reproduce your plot because of all the zeros. Also, I would recommend converting this data to long form. See example code. I would also recommend using a mosaic plot because interval seems to be a categorical variable.

enter image description here

#import the data 
data <- read.csv("C:/test.csv")
colnames(data)[1] <- "steps.1"
data

#convert to long form
data.long <- reshape(data, varying=1:6, direction="long", timevar="steps", sep=".")
data.long$group <- as.factor(rep(c(1:3), each=10))
data.long

#plot
library(ggplot2)
ggplot(data.long, aes(x=interval, y=steps, color=group)) +
  geom_point() 

Upvotes: 2

Related Questions