Discussion:
geom_point() symbols in the legend
Vittorio de martino
2018-03-25 18:51:00 UTC
Permalink
I have the following dataset "vc"
vc
date gross corrected
13 2017-01-01 3.23 1.98
14 2017-02-01 -4.22 -1.62
15 2017-03-01 -1.14 -1.27
16 2017-04-01 1.83 1.10
17 2017-05-01 0.57 0.23
18 2017-06-01 6.09 -0.47
19 2017-07-01 -2.67 0.28
20 2017-08-01 4.68 1.92
21 2017-09-01 -8.66 -0.20
22 2017-10-01 1.69 -1.11
23 2017-11-01 1.77 0.42
24 2017-12-01 0.70 0.85


p <- ggplot(vc) +
scale_x_date(limits=as.Date(c("2016-12-31","2018-01-01")),
date_labels = "%m/%y",date_minor_breaks="1 month") +
scale_y_continuous(breaks=seq(-9,7,by=2)) +
xlab(NULL) +
ylab("Short-term changes (%)") +
theme(panel.background= element_rect(fill="grey90"), text = element_text
(size=7), axis.ticks = element_line(size=0.1),
panel.grid.major = element_line(size = 0.5, linetype = 'solid'
,colour = "grey95"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
colour = "grey95")) +
geom_hline(yintercept=0, color="brown",size=0.5, linetype="solid") +
geom_point(aes(date,gross, color="Gross"), size=1, shape=3) +
geom_point(aes(date,corrected, color="Corrected"), size=1, shape=16) +
theme(legend.position="bottom") +
scale_colour_manual("", breaks=c("Gross","Corrected"),
values=c("Gross"="blue", "Corrected"="red")) +
theme(legend.title = element_text(colour="black", size = 6, face =
"bold"),
legend.text = element_text(colour="black", size = 6, face = "bold"))


ggsave("test.pdf",plot=p,device="pdf", width=18,height=6,units="cm")


This code produces the attached result.

The colors in the legend are correct but the shape aren't. How can I have a
blue "+" and a red point in the legend?

Thanks
Vittorio
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Crump, Ron
2018-03-26 08:44:43 UTC
Permalink
Hi Vittorio,
Post by Vittorio de martino
I have the following dataset "vc"
For future reference it would be easier if the data were supplied as the
output from dput(vc), as this makes it easier to cut and paste while
ensuring we have the same data as you do (eg in terms of factors v
character strings etc).

# eg

vc <- structure(list(date = structure(c(17167, 17198, 17226, 17257,
17287, 17318, 17348, 17379, 17410, 17440, 17471, 17501), class = "Date"),
gross = c(3.23, -4.22, -1.14, 1.83, 0.57, 6.09, -2.67, 4.68,
-8.66, 1.69, 1.77, 0.7), corrected = c(1.98, -1.62, -1.27,
1.1, 0.23, -0.47, 0.28, 1.92, -0.2, -1.11, 0.42, 0.85)), .Names =
c("date",
"gross", "corrected"), row.names = c("13", "14", "15", "16",
"17", "18", "19", "20", "21", "22", "23", "24"), class = "data.frame")

# If you convert your data to 'long' format and let shape and colour
# be controlled by the column defining which records are Gross and which
# are Corrected, then this becomes pretty straightforward:

library(ggplot2)
library(reshape2)
vcm <- melt(vc,id.vars='date')
vcm$variable <- factor(vcm$variable,
levels=c('gross','corrected'),
labels=c('Gross','Corrected'))

# then, keeping most of your code as is and plotting vcm rather than vc,
# and labelling modified lines (#CHANGED):
p <- ggplot(vcm, aes(x=date,y=value))+
scale_x_date(limits=as.Date(c("2016-12-31","2018-01-01")),
date_labels = "%m/%y", date_minor_breaks="1 month") +
scale_y_continuous(breaks=seq(-9,7,by=2)) +
xlab(NULL) +
ylab("Short-term changes (%)") +
theme(panel.background= element_rect(fill="grey90"),
text = element_text(size=7),
axis.ticks = element_line(size=0.1),
panel.grid.major = element_line(size = 0.5,
linetype = 'solid',
colour = "grey95"),
panel.grid.minor = element_line(size = 0.25,
linetype = 'solid',
colour = "grey95")) +
geom_hline(yintercept=0, color="brown",
size=0.5, linetype="solid") +
theme(legend.position="bottom") +
theme(legend.title = element_blank(), #CHANGED
legend.text = element_text(colour="black", size = 6,
face = "bold")) +
#CHANGED next three lines
geom_point(aes(colour=variable,shape=variable),size=1) +
scale_colour_manual(values=c('blue','red')) +
scale_shape_manual(values=c(3,16))


I think that does it.

You could also have simplified the code to make it easier for people to
help you. In this case there is a lot that of theme stuff in particular
that does not relate to the specific question to be considered at and
could just have been left out.

Regards,
Ron.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Vittorio de martino
2018-03-26 13:17:00 UTC
Permalink
Thanks a lot Ron!
I'm an absolute beginner with ggplot2 therefore your solution and above all
your suggestions to make a better question are extremely valuable to me.
Thanks again and
Ciao from Rome
Vittorio

Il giorno domenica 25 marzo 2018 22:14:51 UTC+2, Vittorio de martino ha
Post by Vittorio de martino
I have the following dataset "vc"
vc
date gross corrected
13 2017-01-01 3.23 1.98
14 2017-02-01 -4.22 -1.62
15 2017-03-01 -1.14 -1.27
16 2017-04-01 1.83 1.10
17 2017-05-01 0.57 0.23
18 2017-06-01 6.09 -0.47
19 2017-07-01 -2.67 0.28
20 2017-08-01 4.68 1.92
21 2017-09-01 -8.66 -0.20
22 2017-10-01 1.69 -1.11
23 2017-11-01 1.77 0.42
24 2017-12-01 0.70 0.85
p <- ggplot(vc) +
scale_x_date(limits=as.Date(c("2016-12-31","2018-01-01")),
date_labels = "%m/%y",date_minor_breaks="1 month") +
scale_y_continuous(breaks=seq(-9,7,by=2)) +
xlab(NULL) +
ylab("Short-term changes (%)") +
theme(panel.background= element_rect(fill="grey90"), text =
element_text(size=7), axis.ticks = element_line(size=0.1),
panel.grid.major = element_line(size = 0.5, linetype =
'solid',colour = "grey95"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
colour = "grey95")) +
geom_hline(yintercept=0, color="brown",size=0.5, linetype="solid") +
geom_point(aes(date,gross, color="Gross"), size=1, shape=3) +
geom_point(aes(date,corrected, color="Corrected"), size=1, shape=16) +
theme(legend.position="bottom") +
scale_colour_manual("", breaks=c("Gross","Corrected"),
values=c("Gross"="blue", "Corrected"="red")) +
theme(legend.title = element_text(colour="black", size = 6, face =
"bold"),
legend.text = element_text(colour="black", size = 6, face = "bold"
))
ggsave("test.pdf",plot=p,device="pdf", width=18,height=6,units="cm")
This code produces the attached result.
The colors in the legend are correct but the shape aren't. How can I have
a blue "+" and a red point in the legend?
Thanks
Vittorio
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...