Take the following "Horizontal grid" example from the Plotly ggplot2 Library
I want to omit the labels from the X axes in both subplots, like:
The way to do this should be, allegedly, by using the layout() function and including configuration parameter xaxis = list(showticklabels = FALSE). See however the output in the following reprex:
library(tidyverse)
library(reshape2)
library(plotly)
p <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1) +
facet_grid(. ~ sex)
ggplotly(p) %>%
layout(xaxis = list(showticklabels = FALSE))
As you can see, only the first subplot is affected.
I have already tried by rendering each subplot as a plotly object, and then using the subplot function:
tips %>% group_split(sex) %>% map(
~{
p <- ggplot(., aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)
ggplotly(p)
}
) %>%
subplot(shareY = TRUE) %>%
layout(xaxis = list(showticklabels = FALSE))
The result is the same.
Also, calling layout(., xaxis = list(showticklabels = FALSE)) for each subplot individually fails, as subplot() apparently overrides the layout of the subplots.
Additionally, inspection of the JSON object seems to show that only one layout attribute is generated for the whole subplot, which I understand should control the properties of all subplots.
Any idea on how to solve this? Any help would be much appreciated!
To achieve what you desire, removing all x-axis labels from the ticks, I would remove them using your ggplot theme. Thus before you call ggplotly
p <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) +
geom_point(shape=1) +
facet_grid(. ~ sex) +
theme( axis.text.x=element_blank()) # remove x-labels
p
ggplotly(p)
Related
I am trying to hide the plotly toolbar when displaying a reactive ggplot chart within a Shiny app. Currently my code looks like this:
renderPlotly( ggplot(users_activated(), aes(x = month, y = n)) +
geom_bar(stat = "identity"))
I have tried config(displayModeBar = F) to no avail.
You need to convert your ggplot object to a plotly object before you can render it as one, or use plotly functions on it. ggplotly does that. Below, is an example using mpg dataset.
ggplotly(ggplot(mpg, aes(class, displ, color = manufacturer)) +
geom_point()
) %>%
config(displayModeBar = F)
my plot looks like this
This is what I've tried. I make individual scatter plots and combined them together with grid.arrange.
data(methylmercurydata)
p1 <- ggplot(data=methylmercurydata,aes(x=MeHg, y=logTHg)) + geom_point()
p2 <- ggplot(data=methylmercurydata,aes(x=MeHg, y=OM)) + geom_point()
p3 <- ggplot(data=methylmercurydata,aes(x=MeHg, y=FeRB)) + geom_point()
grid.arrange(p1,p2,p3)
Easiest way to do this would likely be using dplyr::facet_wrap() after creating a longer table.
Something like:
library(tidyverse)
methylmercurydata %>%
pivot_longer(cols = c(logTHg, OM, FeRB), names_to = 'metric') %>%
ggplot() +
geom_point(aes(MeHG, value)) +
facet_wrap(~metric)
Edit: r2evans makes a good point; if you need separate scales across y, you can use scales = 'free_y' within the facet_wrap call. Likewise, scales = 'free_x' would provide different x axes, and scales = 'free' would provide different scales for each axis. One other thing to consider when recreating the above plot would be specifying the ncol argument as well, in this case ncol = 1.
It's not clear to me what you mean by 'scatterplot matrix', but if you want to make a correlation matrix, you can use ggforce (e.g. per https://ihaddadenfodil.com/post/it-s-a-bird-it-s-a-plane-it-s-a-ggforce-function/):
library(tidyverse)
library(ggforce)
library(palmerpenguins)
ggplot(penguins, aes(col = sex)) +
geom_autopoint(na.rm = TRUE) +
facet_matrix(rows = vars(bill_length_mm:body_mass_g),
switch = "x")
Say I have the following data frame:
ret <- rnorm(100, 0, 5)
df <- data.frame(
x = seq(1, 100, 1),
ret = ret,
y = 100 + cumsum(ret),
col = c(ifelse(ret > 0, "red", "forestgreen"), NA)[-1]
)
Here I'm simulating the returns of some fictional financial asset using rnorm named 'ret', and am defining a color vector named 'col' where upticks are green and downticks are red.
What I want to produce is something like the following:
library(ggplot2)
ggplot(df, aes(x=x, y=y)) + geom_line(aes(colour=col, group=1))
But I want to make a similar image using plotly so that I can zoom in on sections of the plot. My first thought was to try simply using the ggplotly() function around the code that produced the desired image:
library(plotly)
ggplotly(ggplot(df, aes(x=x, y=y)) + geom_line(aes(colour=col, group=1)))
But the plot is no longer grouped. Additionally, I tried using plot_ly() but can't seem to make the line segments get their color according to the 'col' attribute that I'm specifying:
plot_ly(data=df, x = ~x) %>% add_lines(y = ~y, line = list(color=~col))
But my color argument doesn't affect the color of the line. I've tried various other things but keep ending up with one of the two undesired plots. Any help would be much appreciated!
Note: I've already made candlestick and OHLC charts with plot_ly(), but I can't work with them because the y axis doesn't scale when you zoom in to a subsection of the plot.
I was able to get the desired behaviour from ggplotly by using geom_segment and making each segment link up to the next (x, y) value, regardless of colour:
library(dplyr)
df = df %>%
arrange(x) %>%
mutate(x_next = lead(x), y_next = lead(y))
p = ggplot(df, aes(x=x, y=y)) +
geom_segment(aes(xend = x_next, yend = y_next, colour=col))
ggplotly(p)
That said, I don't have a good answer for why ggplotly doesn't produce the desired output in the first place.
In the following shiny app, I want to add some spaces between mainPanel and a siderbarPanel in the bottom.
Here, I can't read the x axis ticks, I tried to rotate them but it doesnt improve the thing.
I also changed the height and width of the plotlyOutput but doesn't work.
I tried to add an HTML("<br><br><br>") but doesnt work too, the problem is probably from ggplot or plotly ?
My ggplot script is :
ggplot(data = df, aes(x = perimetre_commercial_estime,
y = nbr_ligne,
fill = perimetre_commercial_estime)) +
geom_bar(stat="identity") + theme_light() +
theme(axis.text.x=element_text(angle=30,hjust=1,vjust=0.5,size=6),
axis.title.x=element_blank(),
legend.position="top", legend.direction="horizontal") +
scale_fill_discrete("")
plotly doesnt support horizontal legend, so the graphic below is normal.
Thanks
This Plotly Setting Graph Size in R article might be helpful. Since you don't include all code, I cannot confirm entirely. Please let me know.
library(plotly)
library(dplyr)
gp = ggplot(
data = mtcars %>% add_rownames(),
aes( x = rowname, y = mpg )
) + geom_point() +
theme(axis.text.x=element_text(angle=30,hjust=1,vjust=0.5,size=6),
axis.title.x=element_blank())
ggplotly(gp) %>%
layout(
margin = list(
b=100
)
)
My question relates to plots in ggplot. Running the code below each image should work if you load the "diamonds" dataset that comes with ggplot2.
I am trying to generate a graph like this:
library(ggplot2)
#First plot
p1 <- ggplot(diamonds, aes(color)) + geom_bar(aes(group = cut, y = ..density..))
p1 <- p1 + facet_wrap(~cut)
p1
but I want to color each bar in each facet by factor, like in this plot:
#Second plot
p2 <- ggplot(diamonds, aes(color)) + geom_bar(aes( y = ..density.., fill = color))
p2 <- p2 + facet_wrap(~cut)
p2
The problem is that "group =" and "fill=" appear to interfere with each other when I attempt to call them both; ggplot seems to ignore the "fill" command when "group" is also called.
The call to group is important because it forces the y-axis to scale for each facet, so that densities within each facet add up to 1. However, I'd like to be able to visually distinguish between groups easily using fill colors.
How can I work around this?
The problem is with ..density... It often is a convenient shortcut, but in a more complicated situation like this one it's often easier just to calculate on your own:
library(dplyr)
diam2 <- diamonds %>% group_by(cut) %>%
mutate(ncut = n()) %>%
group_by(cut, color) %>%
summarize(den = n() / first(ncut))
ggplot(diam2, aes(x = color, fill = color, y = den)) +
geom_bar(stat = "identity") +
facet_wrap(~ cut)
I should add, comparing my plot with your p1, the shapes are the same but the scale looks a little different (mine being a little lower overall). I'm not sure why.