changing plot dimensions in ggplot2 with one categorical variable - r

I am trying to plot data with a categorical x-axis variable and a continuous y-axis variable. The current plot is shown below:
A am trying to alter the height of the y-axis to make the plot taller and thinner. I know you can do this kind of thing with a continuous variable vs. continuous variable scatterplot using coord_fixed(), e.g.:
However, this works on the numeric ratios of the x and y data, which doesn't apply if one variable is categorical. Trying coord_fixed on my data with any input seems to just scale the plot exactly to the plot area:
Trying to adjust the canvas at the ggsave phase just adds white space around the plot, rather than changing the plot shape itself, which isn't what I'm looking for either:
ggsave(filename = paste(imgSaveDir,'RT_data_summ.png',sep=""),width=7,height=10)
Any help is appreciated!

Related

Swap Axes of Violin plot

I am trying to create Violin Plots using the StatsPlots.jl library.
However, I would like to have the returned Violin plot to be horizontal instead of vertical as I want to show the distribution of a variable (e.g. Temperature) for different heights, eg. at 1000m, 2000m, 3000m ...
So it would be nice if the height was at the y-Axis while the temperature distribution was on the x-Axis.
Is there a way to swap the axes of a Plots.Plot struct, or is there an argument I could pass to violin() that does the trick?

R plot with plotly: Changing value on scale for 2dHeatMap

I used the following lines to plot a heatmap with plotly in R:
plot_ly(data, x , y) %>% add_trace(type='histogram2dcontour')
I then obtained the following plot
The data actually looks like that on a scatter plot
As you can see, I lose a lot of points on the heatmap. I was wondering how I could manually set the scale for the colour of the heatmap for e.g. making it so that the colour changes every time the count increase by 10 instead of 100.
Otherwise, is there a better way to plot and visualize such data?

How to have geom_point and geom_bar side by side and with facet_wrap

I am trying to combine a scatterplot with a bar plot. The bar plot should be on the right side of the scatterplot of each corresponding group specified in facet wrap. The two plots have the same unit on the y axis but the scaling of the bar might need to be adjusted so the barplot it's not too tall.
Here's an example.
data(mpg)
So far I have tried this, but I would the bar should be outside the scatterplot area. Additionally, in my data the x axis in the barplot is a factor with two levels, so it has a discrete scale and my scatterplot has a continuous x scale.
ggplot()+geom_bar(data=mpg,aes(x=10,y=displ,fill=trans),stat='identity',position='stack')
+geom_point(data=mpg,aes(x=displ,y=displ))+facet_wrap(~cyl)
Can I achieve to plot these two plot types side-by-side or do I have to use something like cowplot? I think that if I have to combine plots outside ggplot I will not be able to use factor_wrap, so I guess I have to do a for loop for each factor level and then combine everything?

How to create a boxplot with log10 scale and zero values in ggplot2

I am creating several boxplots in ggplot2 with a log10 scale using
coord_trans(y="log10")
It is important that only the scale and not the data itself is log-transformed. One data set includes zero values, which is creating -inf values so that the boxplot cannot be drawn on a log-transformed scale.
I have tried to use
scale_y_continuous(trans=pseudo_log_trans(base=10))
However, this makes changes to the data instead of the scale. Outliers of the boxplot change and the boxplot stats extracted through ggplot_build(examplefig)$data are different from the original data.
Is there any way to create a boxplot in ggplot2 with a log10 scale and data including zero values? There should be no transformation of the data itself and outliers should be displayed like in the boxplot with the original data.
This is the very first question I ask here and I am new to R, so I hope the question is clear.

Set ticks margin on one axis (ggplot2)

When plotting graphs with categorical variables (such as boxplots) with long names, the names have to be shifted using the theme command in ggplot2, then the distance between the axis ticks and the text can be set as well yet this distance is reflected on both axis when it is some time only necessary on one axis. Below some sample code:
df<-data.frame(X=rnorm(50,0,10),Y=c(rep("Some Text",25),rep("Some Larger Text That Takes Space",25)))
#classical boxplots
ggplot(df,aes(x=Y,y=X))+geom_boxplot()+theme(axis.text=element_text(size=20),axis.text.x=element_text(angle=45))
#the x axis labels need to be shifted downwards
ggplot(df,aes(x=Y,y=X))+geom_boxplot()+theme(axis.text=element_text(size=20),axis.text.x=element_text(angle=45),axis.ticks.margin=unit(4,"cm"))
#now they are shifted but there is unnecessary space on the y-axis
How can we set axis.ticks.margin to act on only one axis?
Try this for example :
library(grid)
axis.ticks.margin=unit(c(4,-4),'cm'))
So, the ggplot2 call becomes:
ggplot(df,aes(x=Y,y=X))+
geom_boxplot()+
theme(axis.text=element_text(size=20),
axis.text.x=element_text(angle=45),
axis.ticks.margin=unit(c(4,-4),'cm'))

Resources