简体   繁体   English

无法更改图例框中的文本标签,并使用ggplot在图例中显示不同的颜色

[英]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 我试图绘制step.1与interval.1之间的图表 在此输入图像描述 ggplot ggplot

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? 但是图例框中会出现随机颜色,即,如果我给出的颜色为绿色,则会绘制不同的颜色,我无法更改图例框中每个文本标签的名称?

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. 我还建议使用马赛克图,因为区间似乎是一个分类变量。

在此输入图像描述

#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() 

Please try this code, instead of giving color, keep variables names as is in color (eg: color = interval.1) 请尝试此代码,而不是给出颜色,保持变量名称的颜色(例如: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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM