Reputation: 556
Using the code below, I could create the plot shown in the image. I am trying to fix some issues as follows:
1- How can I place the legend between title and plot. I would like to have the legend in a horizontal format rather than the current vertical form.
2- How can I bold, and center the plot title?
3- How can use different shapes in the geom_point function, depending on the "Disability" variable?
4- How can I rename the label so it reads "Age at Death" instead of the current name: "AgeatDeath"?
Thanks,
Nader
IDD_line <-
ggplot(IDD_plot) +
geom_line(aes(x = Year, y = AgeatDeath, color = Disability), size = 1) +
geom_point(aes(x = Year,
y = AgeatDeath,
color = Disability),
size = 1.5,
shape = 21) +
scale_x_continuous(breaks = seq(2008, 2017, 1)) +
labs(
x = "Year",
y = "Age at Death",
caption = (""),
face = "bold"
) +
theme_bw() +
scale_y_continuous(breaks = seq(35, 80, 5)) +
ggtitle(paste("Age of Death by Year (2008 to 2017)")) +
theme(
legend.title = element_blank(),
axis.text = (blue.bold.10.text),
legend.position = "top"
)
output$IDD_plot <- renderPlotly(ggplotly(IDD_line) %>%
config(displayModeBar = F))
Upvotes: 0
Views: 364
Reputation: 30474
This was a bit harder than I initially anticipated. Maybe somebody else will come up with some better solutions, but here's what I have.
You can do this in layout
after call to ggplotly
. To put into horizontal form, use orientation = "h"
. If you specify y = 1.2
this will go above the plot (if you just set yanchor
at "top" this will go on the plot itself near the top).
In addition, you'll want to add extra margin so the legend does not overlap with the title. Use margin
to set left, right, bottom, and top margins (l
, r
, b
, and t
).
layout(legend = list(x = 0, y = 1.2, orientation = "h"),
margin = list(l = 50, r = 50, b = 100, t = 100)...
To bold the title, you can add HTML tags, such as <b>title</b>
in ggtitle
. To center the title, add that to layout
as in #1, using xanchor
:
layout(legend = list(x = 0, y = 1.2, orientation = "h"),
margin = list(l = 50, r = 50, b = 100, t = 100),
title = list(xanchor = "center", x = .5))
To get the shape to depend on "Disability", you will need shape = Disability
in your aesthetic. The tricky part is that if you do this inside aes
inside geom_point
, you will get duplicate legends after converting to ggplotly
. You can get around this with a separate aes
outside of geom_point
.
For more information see this github issue comment.
One method is to use text
in your aes
and clarify your labels this way. This will include line breaks with <br>
, %d
for integer values, and %s
for string (for Disability):
text = sprintf("Year: %d<br>Age at Death: %d<br>Disability: %s",
Year, AgeatDeath, Disability)
Then, in your ggplotly
call, include tooltip = "text"
.
So putting this all together, you have:
IDD_line <- ggplot(IDD_plot) +
aes(color = Disability,
shape = Disability,
group = Disability,
text = sprintf("Year: %d<br>Age at Death: %d<br>Disability: %s",
Year, AgeatDeath, Disability)) +
geom_line(aes(x = Year, y = AgeatDeath), size = 1) +
geom_point(aes(x = Year,
y = AgeatDeath),
size = 1.5) +
scale_x_continuous(breaks = seq(2008, 2017, 1)) +
labs(
x = "Year",
y = "Age at Death",
caption = (""),
face = "bold"
) +
theme_bw() +
scale_y_continuous(breaks = seq(35, 80, 5)) +
ggtitle(paste("<b>Age of Death by Year (2008 to 2017)</b>")) +
theme(
legend.title = element_blank(),
axis.text = (blue.bold.10.text) #,
#legend.position = "top"
)
And,
output$IDD_plot <- renderPlotly(
ggplotly(IDD_line, tooltip = "text") %>%
config(displayModeBar = F) %>%
layout(legend = list(x = 0, y = 1.2, orientation = "h"),
margin = list(l = 50, r = 50, b = 100, t = 100),
title = list(xanchor = "center", x = .5))
)
Upvotes: 1