Ich möchte Linien mit verschiedenen Formen mit mehr als sechs Sätze von Daten, mit diskreten Farben. Die Probleme sind 1) es wird eine andere Legende für Linienfarbe und -form generiert, sollte aber nur eine Legende mit der Linienfarbe und -form sein, 2) wenn der Titel für die Linienfarbenlegende korrigiert wird, verschwindet die Farbe.Mehr als sechs Formen in ggplot
t=seq(0,360,20)
for (ip in seq(0,10)) {
if (ip==0) {
df<-data.frame(t=t,y=sin(t*pi/180)+ip/2,sn=ip+100)
} else {
tdf<-data.frame(t=t,y=sin(t*pi/180)+ip/2,sn=ip+100)
df<-rbind(df,tdf)
}
}
head(df)
# No plot
# Error: A continuous variable can not be mapped to shape
gp <- ggplot(df,aes(x=t,y=y,group=sn,color=sn,shape=sn))
gp <- gp + labs(title = "Demo more than 6 shapes", x="Theat (deg)", y="Magnitude")
gp <- gp + geom_line() + geom_point()
print(gp)
# No plot
# Error: A continuous variable can not be mapped to shape (doesn't like integers)
gp <- ggplot(df,aes(x=t,y=y,group=sn,color=sn,shape=as.integer(sn)))
gp <- gp + labs(title = "Demo more than 6 shapes", x="Theat (deg)", y="Magnitude")
gp <- gp + geom_line() + geom_point()
print(gp)
# Gives warning about 6 shapes, and only shows 6 shapes, continous sn colors
gp <- ggplot(df,aes(x=t,y=y,group=sn,color=sn,shape=as.factor(sn)))
gp <- gp + labs(title = "Only shows six shapes, and two legends, need discrete colors",
x="Theat (deg)", y="Magnitude")
gp <- gp + geom_line() + geom_point()
print(gp)
# This is close to what is desired, but correct legend title and combine legends
gp <- ggplot(df,aes(x=t,y=y,group=sn,color=as.factor(sn),shape=as.factor(sn %% 6)))
gp <- gp + labs(title = "Need to combine legends and correct legend title", x="Theat (deg)", y="Magnitude")
gp <- gp + geom_line() + geom_point()
print(gp)
# Correct legend title, but now the line color disappears
gp <- ggplot(df,aes(x=t,y=y,group=sn,color=as.factor(sn),shape=as.factor(sn %% 6)))
gp <- gp + labs(title = "Color disappeard, but legend title changed", x="Theat (deg)", y="Magnitude")
gp <- gp + geom_line() + geom_point()
gp <- gp + scale_color_manual("SN",values=as.factor(df$sn))
print(gp)
# Add color and shape in geom_line/geom_point commands,
gp <- ggplot(df,aes(x=t,y=y,group=sn))
gp <- gp + labs(title = "This is close, but legend symbols are wrong", x="Theat (deg)", y="Magnitude")
gp <- gp + geom_line(aes(color=as.factor(df$sn)))
gp <- gp + geom_point(color=as.factor(df$sn),shape=as.factor(df$sn %% 6))
gp <- gp + scale_color_manual("SN",values=as.factor(df$sn))
print(gp)
möglich Duplikat [Ändern der Formen für Skala \ _shape() in ggplot2 verwendet] (http://stackoverflow.com/questions/1478532/changing-shapes-used-for-scale-shape-in-ggplot2) – Henrik
Ich glaube nicht, dass diese Frage ein Duplikat dieser Frage ist, er spricht nur davon, die Form über den Bereich der Formen zu ändern. Mein Problem ist, dass die Anzahl der benötigten Formen die Anzahl der verfügbaren Formen übersteigt. Diese Frage ist ein Duplikat von http://stackoverflow.com/questions/26223857/more-than-six-shapes-in-ggplot, aber es gibt keine Antwort auf diese Frage. – user3969377