Maintaining linked zoom between subplots using plotly - r

I would like to create a facet plot with ggplot and show the x-axis with ticks on all facets. Then I want to use ggplotly to convert the result to a plotly graph. However when I use scales='free_x' to add the extra x-axes to the facets, the zoom linkage is lost. i.e. when I zoom on one of the facets it doesn't zoom on the others automatically. Is there a way to add that functionality back in?
This is a reprex showing the problem:
library(ggplot2)
library(ggthemes)
library(plotly)
p <- ggplot(mtcars, aes(mpg, hp)) + geom_point() + facet_wrap(~carb, ncol = 1, scales='free_x') +
theme_tufte() + theme(axis.line=element_line()) +
scale_x_continuous(limits=c(10,35)) + scale_y_continuous(limits=c(0,400))
ggplotly(p)
Notice here when I zoom in on the top plot, the other plots remain unchanged:

Related

legend ticks outside of the key box in ggplot2

I'm trying to generate a "satisfactory" legend using ggplot2, but what frustrates me is the position of ticks (they are inside the legend key). Here is some code to show what I mean.
library(ggplot2)
ggplot(mtcars, aes(x = cyl, y = gear, fill = mpg)) +
geom_tile() +
coord_equal() +
theme(legend.position = 'top')
What I get is below. The ticks are inside the legend key box.
Is there a way to put the ticks outside of the legend key box? Just like this legend (it look like they did this with ggplot2):
Any suggestion would be appreciated!!!

How to prevent x axes overlap ggplotly with facet_wrap(~variable)

I have some data that looks nice when plotted with ggpglot but the x-axes and the plots underneath overlap in plotly::ggplotly().
library(gapminder)
library(plotly)
p <- ggplot(gapminder, aes(x=gdpPercap, y=lifeExp)) + geom_point() + scale_x_log10()
p <- p + aes(color=continent) + facet_wrap(~year,scale="free")
gp <- ggplotly(p)
gp
So I've been trying this: R: ggplot and plotly axis margin won't change
Basically I need to increase the white space between each graph object and the one below. Any thoughts would be much appreciated.
You can add vertical space in between your facets using:
theme(panel.spacing.y = unit(1, "line")) # adjust to taste
However, there's a snag related to axis titles and facets when converting to plotly, where the axis title becomes an annotation and needs to be shifted more manually.
See these links for ways how:
https://stackoverflow.com/a/47228372/6851825
Converting ggplot object to plotly object creates axis title that overlaps tick values
https://github.com/ropensci/plotly/issues/1224

ggolot horizontal gaps contour

I've been trying to plot contour plots using ggplot2 and csv file. I can't figure out why there are horizontal gaps showing up across the image.
Here is the code:
library(ggplot2)
plot2 <- ggplot(data=thirtyfour,aes( x = X.m., y = Z.m., z = t_ca.2))
plot2
plot2 + geom_tile(aes(fill = t_ca.2) )+
scale_fill_continuous(limits=c(0,0.0219),
breaks=seq(0,0.0219, by=0.01),
low="blue",
high="yellow")
In the geom_tile aesthetic try adjusting the height parameter.
+geom_tile(aes(fill=t_ca.2, height=1)) +...
Otherwise, please provide reproducible example code.

how can i show the corresonding number in the picture from legend in ggplot2

I used the code ggplot(product_info,aes(x=lat,y=lon,colour=factor(location)))+geom_point()
,picture like this
I want to the number from legend show in the picture with these colourful point together.
You can show the text for the corresponding factor using geom_text(). Here's an example with the diamonds data-set:
ggplot(diamonds, aes(x=price, y=carat, colour=factor(cut), label=factor(cut))) +
geom_point() +
geom_text()
Alternatively, you could consider ggplotly which would provide an easier way of viewing the data
library(plotly)
p <- ggplot(diamonds, aes(x=price, y=carat, colour=factor(cut)))
+ geom_point()
ggplotly(p)

Dividing lines between sections of bar plot with ggplot2 in r

I am using qplot to create a stacked bar plot and would like to place a white line between the sections of each bar as the blues seem to almost blend together. I do not want to change my existing colour scheme to resolve the problem. Any ideas?
library(ggplot2)
qplot(carat, data = diamonds, geom = "histogram", fill = color)
Add the argument colour="white" to create a white outline:
ggplot(mtcars, aes(factor(cyl), fill=am, group=am)) + geom_bar(colour="white")
Here is a workaround to remove the diagonal line from the legend (inspired by a posting on ggplot mailing list). The idea is to plot the geom_bar twice, once suppressing the colours:
ggplot(mtcars, aes(factor(cyl), fill=am, group=am)) +
geom_bar() +
geom_bar(colour="white", show_guide=FALSE)

Resources