Plot must contain exactly one panel; marginal box plot on faceted scatterplot - r

I am very new to programming, but I managed to get around making faceted scatterplots and marginal box plots for scatter plots using advice in the link below: http://www.lreding.com/nonstandard_deviations/2017/08/19/cowmarg/
My question is how can I make marginal boxplots on my faceted scatterplot?
My code is:
CN<-read.csv("LfFlw.csv")
library(ggplot2)
Simple scatter plot:
ggplot(data=CN, aes(x=PlantOrder, y=CN, colour=Tissue))+geom_point()+facet_wrap(~Population, scales="free_x", nc=2)
Scatterplot black and white:
sc<-ggplot(data=CN, aes(x=PlantOrder, y=CN, shape=Tissue))+geom_point()+facet_wrap(~Population, scales="free_x", nc=2)
sc
Scatter plot with labelled axes:
sc_lab<-sc+labs(x="Individual plants (ordered)", y="Cyanide (ug g^-1 dw)")
sc_lab
Scatter plot with labelled axes and classic theme:
sc_lab_th<-sc_lab+theme_classic()
sc_lab_th
Scatter plot with labelled axes and classic theme with changed shapes:
s<-sc_lab_th+scale_shape_manual(values=c(8,2))
s
Boxplot with facet and white/grey:
y_box <- axis_canvas(s, axis = "y") + geom_boxplot(data = CN, outlier.shape = 1, aes(x = 0, y = CN, fill=Tissue)) +
facet_wrap(~Population, scales="free_x", nc=2)+scale_fill_manual(values=c("white", "grey"))
y_box
library(cowplot)
ggdraw(insert_yaxis_grob(s, y_box, position = "left"))
And here I end up with an error:
Error in get_panel(grob) : Plot must contain exactly one panel

The answer is: You can't. (At least not via the axis_canvas() / insert_yaxis_grob() route. The error message tells you exactly what's going on: The function insert_yaxis_grob() can only insert plots that consist of a single panel. You've made a faceted plot, which contains multiple panels.

Related

Stack dotplot labels using ggplot

I am trying to create a simple dotplot that contains 100+ points, some of which are grouped close together. I need the points to be individually labeled, but I would like to stack the labels for points that are close together on top of each other.
Basically, I would like to create a similar dotplot with labeling to the graph below.
As a code example, consider the following code where I would like to add the car name to the dotplot in a way similar to the graphic.
ggplot(mtcars, aes(x = mpg)) +
geom_dotplot(binwidth = .4, stackdir = "centerwhole") +
scale_y_continuous(NULL, breaks = NULL)

How do you remove extra space between x-axis and plot for a geom_density_ridges_gradient() plot in R ggplot

I am making a plot using ggplot2 in R. I am using the ggridges package and the geom_density_ridges_gradient() plot type. These plots create a large space between the bottom of the figure and the labels of the x-axis. Is there a way to remove or shrink this added space?
Here is the code I'm using to make the plot.
library(tidyverse)
library(ggridges)
library(scales)
sample_q <- ggplot(data, aes(x=value/1000,y=factor(category),group=category)) +
geom_density_ridges_gradient(scale=.5) +
theme_ridges(center_axis_labels = TRUE,grid=TRUE) +
xlab("Values") +
ylab("Height") +
theme(text=element_text(size=10)) +
scale_x_continuous(expand = c(0,0), limits = c(-15,15), breaks=c(-15,-10,-5,0,5,10,15))
I've tried creating the plot with different values of 'scale'. Here are three plots with scale set equal to 2, 1, and .5.
You can see that the space between the bottom of the density plot and the x-axis labels is large and does not change with scales. I've tried without the theme_ridges line as well and the plot still has the same vertical space between the density plot and the x-axis labels. I'd like the x-axis labels to be just below the 'Group A' plot.

R: adjusting legend labels to the selected (gg)plotly output

I am using ggplotly object to visualize a scatterplot in a shiny dashboard. I have a plot colored using the values of a column. However, when I want to look at a certain part of the plot, I zoom in to know more about the points. Then the legend labels should adjust according to the points present in the visible window or selected/chosen region.
For example, I have a scatterplot below with full data and the cut column has five different values.
library(plotly)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
p <- ggplot(data = d, aes(x = carat, y = price, color = cut)) + geom_point()
ggplotly(p)
The output:
When I select a window of the plot, there are no points related to Fair or Good in the above plot. How to avoid them in the legend labels? How to adjust/update the labels to the selected region(example, as shown below)? Should this handled using shiny reactive features?

plotly labels in R stacked area chart

I am trying to make a stacked area chart in R exactly like this ggplot2 one (below) only using plotly.
Here is a link to my data.
To generate a plotly version of the above ggplot2 chart, I first have to add the values of each column in my dataframe, elw, on top of the values in the previous column like so. This is because plotly (as far as I'm aware) does not have the ability to automatically stack values in area charts.
With this new stacked data set, elw_stack, I use the following code to make my plotly chart:
el_plot2 = ggplot() +
geom_area(aes(elw_stack$year, elw_stack$x99999, fill = 'green')) +
geom_area(aes(elw_stack$year, elw_stack$x20000, fill = 'red')) +
geom_area(aes(elw_stack$year, elw_stack$x19000, fill = 'blue')) +
geom_area(aes(elw_stack$year, elw_stack$x12018, fill = 'purple')) +
geom_area(aes(elw_stack$year, elw_stack$x10006, fill = 'yellow'))
ggplotly(el_plot2)
That code generates this chart:
The issue is that the plotly labels refer to the cumulative elw_stack values. The green value pictured at year 1999 is actually ~3700 (i.e. 11,365 - 7957). But the description bar says the cumulative value of 11,365. Is there a way to fix this so that the labels aren't cumulative values?
I was having a similar problem and eventually decided not to use ggplotly, but instead i used the plot_ly function. Here is the code I used with your data:
elw <- read.csv("elw.csv")
elw_stack <- read.csv("elw_stack.csv")
plot <- plot_ly(data=elw_stack, x=year, y=x10006, fill="tonexty", mode="lines",
text=round(elw$x10006, 0), hoverinfo='x+text+name', name="x10006")
plot <- add_trace(plot, data=elw_stack, x=year, y=x12018, fill="tonexty", mode="lines",
text=round(elw$x12018,0), hoverinfo='x+text+name', name="x12018")
plot <- add_trace(plot, data=elw_stack, x=year, y=x19000, fill="tonexty", mode="lines",
text=round(elw$x19000,0), hoverinfo='x+text+name', name="x19000")
plot <- add_trace(plot, data=elw_stack, x=year, y=x20000, fill="tonexty", mode="lines",
text=round(elw$x20000,0), hoverinfo='x+text+name', name="x20000")
plot <- add_trace(plot, data=elw_stack, x=year, y=x99999, fill="tonexty", mode="lines",
text=round(elw$x99999,0), hoverinfo='x+text+name', name="x99999")
plot <- layout(plot, yaxis=list(title="Whatever title you wanna use"))
And this is how the final plot looks:
plotly image
What I can't get to work is to add the different traces using a for loop. I wanted to write a function that takes a data frame with an arbitrary number of columns as input and returns the stacked area plot, but for some reason the plot won't show all the traces (only first and last)
Hope it helps...

Scatter plot and boxplot overlay

Based on the previous post ggplot boxplots with scatterplot overlay (same variables),
I would like to have one boxplot for each day of week instead of two boxplots while have scatter points on it with different colour.
The code will be like:
#Box-plot for day of week effect
plot1<-ggplot(data=dodgers, aes(x=ordered_day_of_week, y=Attend)) + geom_boxplot()
#Scatter with specific colors for day of week
plot2<-ggplot(dodgers, aes(x=ordered_month, y=Attend, colour=Bobblehead, size=1.5)) + geom_point()
#Box-ploy with Scatter plot overlay
plot3<-ggplot(data=dodgers, aes(x=ordered_day_of_week, y=Attend, colour=Bobblehead)) + geom_boxplot() + geom_point()
And the result would be:
1, scatter plot
2, boxplot plot
3, combined plot
Put color= inside the aes() of geom_point() and remove it from ggplot() aes(). If you put color= inside the ggplot() then it affects all geoms. Also you could consider to use position dodge to separate points.
Example with mtcars data as OP didn't provide data.
ggplot(mtcars,aes(factor(cyl),mpg))+geom_boxplot()+
geom_point(aes(color=factor(am)),position=position_dodge(width=0.5))

Resources