Reputation: 269
I made a plot using following code in ggplot. Here is the data source https://www.dropbox.com/s/xzauims7rqwurga/who_ghg_complete.csv?dl=0
p1 <- ghg_complete %>%
filter(`Country Name` %in% c("United States", "China", "India")) %>%
ggplot(aes(x = year, y = Emissions,
group = `Country Name`, color = `Country Name`)) +
geom_point(size = 2, shape = 21) +
geom_line() +
labs(title = "Greenhouse gas emissions (kt of CO2 equivalent)",
x = NULL,
y = NULL) +
scale_x_discrete(breaks = seq(1970, 2012, by = 5)) +
theme_bw() +
theme(legend.position = "bottom") +
scale_color_brewer(palette = "Set1")
p1
Here is the output:- When I wrap the same plot in ggplotly
ggplotly(p1)
The plot behaves differently. The legends don't look the same and the numbers in y-axis is clipped off.
I changed the y-axis numbering as suggested in comments that still didn't help with the issue. Here is the code:
p1 <- ghg_complete %>%
filter(`Country Name` %in% c("United States", "China", "India", "Nepal")) %>%
ggplot(aes(x = year, y = Emissions,
group = `Country Name`, color = `Country Name`)) +
geom_point(size = 2, shape = 21) +
geom_line() +
labs(title = "Greenhouse gas emissions (kt of CO2 equivalent)",
x = NULL,
y = NULL) +
scale_x_discrete(breaks = seq(1970, 2012, by = 5)) +
theme_bw() +
scale_color_brewer(palette = "Set1")
# Convert ggplot object to plotly
gg <- plotly_build(p1) %>%
layout(legend = list(orientation = 'h', y = -0.1))
gg
Upvotes: 0
Views: 808
Reputation: 1293
ggplotly()
doesn't exactly copy your ggplot graph into plotly, it makes a plotly equivalent. If you want to make sure your plotly graph looks like your ggplot graph you will need to adjust it further in plotly. For example to get the legend to look more alike you can add %>% layout(legend = list(orientation = 'h', x = 0.35, y = -0.1))
to your ggplotly
call. You might have to tweak the x
and y
values to line the legend up exactly as you like.
As Mike Wise suggests in the comments for the clipping of the axis labels, you're probably better off just representing the values in megatons or gigatons so that they don't have so many 0s
Upvotes: 1