R / Sciplot: baffi sovrapposti in lineplot.ci
-
28-10-2019 - |
Domanda
Quando si effettuano un diagramma di interazione usando lineplot.ci nella libreria Sciplot, le barre di errore possono sovrapporsi tra i gruppi. Per esempio,
data = c(1,5,3,7,3,7,5,9)
grp1 = c(1,1,1,1,2,2,2,2)
grp2 = c(1,1,2,2,1,1,2,2)
lineplot.CI(grp1, data, grp2)
I gruppi possono essere separati lungo l'asse X aggiungendo jitter alla variabile di raggruppamento e impostando X.cont su True, ma questo fa scomparire le linee nella trama:
data = c(1,5,3,7,3,7,5,9)
grp1 = c(1,1,1,1,2,2,2,2) + c(-0.05, -0.05, 0.05, 0.05, -0.05, -0.05, 0.05, 0.05)
grp2 = c(1,1,2,2,1,1,2,2)
lineplot.CI(grp1, data, grp2, x.cont=TRUE)
È possibile far apparire le righe e per jitter i punti, in modo che le barre di errore non si sovrappongano? O esiste un modo migliore per realizzare questo tipo di trama?
Soluzione
Puoi usare ggplot2 per quello. Ecco un esempio con un set di dati integrato (in quanto non ho i tuoi errori standard o cis). La chiave è usare position_dodge()
.
ToothGrowth$dose.cat <- factor(ToothGrowth$dose, labels=paste("d", 1:3, sep=""))
df <- with(ToothGrowth , aggregate(len, list(supp=supp, dose=dose.cat), mean))
df$se <- with(ToothGrowth , aggregate(len, list(supp=supp, dose=dose.cat),
function(x) sd(x)/sqrt(10)))[,3]
opar <- theme_update(panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),
panel.background = theme_rect(colour = "black"))
xgap <- position_dodge(0.2)
gp <- ggplot(df, aes(x=dose, y=x, colour=supp, group=supp))
gp + geom_line(aes(linetype=supp), size=.6, position=xgap) +
geom_point(aes(shape=supp), size=3, position=xgap) +
geom_errorbar(aes(ymax=x+se, ymin=x-se), width=.1, position=xgap)
theme_set(opar)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow