[英]Unable to change text labels in legend box and showing up different color in legend using ggplot
我的數據框是
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.................
.
.
.
.
我試圖繪制step.1與interval.1之間的圖表 ggplot
我的代碼
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"))
但是圖例框中會出現隨機顏色,即,如果我給出的顏色為綠色,則會繪制不同的顏色,我無法更改圖例框中每個文本標簽的名稱?
由於所有的零,我無法重現你的情節。 另外,我建議將此數據轉換為長格式。 請參閱示例代碼。 我還建議使用馬賽克圖,因為區間似乎是一個分類變量。
#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()
請嘗試此代碼,而不是給出顏色,保持變量名稱的顏色(例如:color = interval.1)
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
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.