I cannot seem to get my ggparcoord
plot to color using a discrete scale. When I do this:
ggparcoord(data = iris, columns = 1:4, groupColumn = "Species")
the output plot is still coloring the lines using a continuous scale (using the levels of the Species
factor).
I've also tried a modified version of the the scale_color_manual
trick specified here: controlling color in ggparcoord, to no avail.
ggparcoord(data = iris, columns = 1:4, groupColumn = "Species") +
scale_color_manual(values = c("setosa" = "red",
"versicolor" = "green",
"virginica" = "blue"))
But I get this error message: Error: Continuous value supplied to discrete scale
.
I've also tried .. + scale_color_discrete()
: same error message.
I'm stumped... even the examples on the ggparcorod cran page aren't working:
data(diamonds, package="ggplot2")
diamonds.samp <- diamonds[sample(1:dim(diamonds)[1],100),]
ggparcoord(data = diamonds.samp,columns = c(1,5:10),groupColumn = 2)
Error message: Error: (list) object cannot be coerced to type 'double'
Edit
There is a solution to this which is to use mapping=aes(color=as.factor(Species))
, as shown in Didzis Elferts' answer below. But, it requires adding some further customizations in scale_color_discrete
ggparcoord(data = iris, columns = 1:4, mapping=aes(color=as.factor(Species))) +
scale_color_discrete("Species", labels=levels(iris$Species) )
Is there a method of accomplishing the same thing without needing to add the scale_color_discrete("Species", labels=levels(iris$Species) )
part?
For example, in ggplot2
,
ggplot(data=iris, aes(x=Sepal.Width,y=Sepal.Length,color=Species)) + geom_point()
automatically generates nicely labeled legends. Is there an analogous method in ggparcoord
?
I am using R version 3.2.1, ggplot2_2.0.0
, and GGally_1.0.0
sessionInfo()
# R version 3.2.1 (2015-06-18)
# Platform: x86_64-apple-darwin13.4.0 (64-bit)
# Running under: OS X 10.10.5 (Yosemite)
#
# locale:
# [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] ggplot2_2.0.0 GGally_1.0.0 magrittr_1.5 dplyr_0.4.3
Any help would be much appreciated!
One solution is to use argument mapping=
and set it to as.factor(Species)
(without as.factor()
it gives the same result as in question). Then with scale_color_discrete()
you can get Species
names as labels.
ggparcoord(data = iris, columns = 1:4, mapping=aes(color=as.factor(Species)))+
scale_color_discrete("Species",labels=levels(iris$Species))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With