Add space between mainPanel and sideerbarPanel in shiny - r

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
)
)

Related

Ggplotly missing text on bars [duplicate]

I am using plotly 4.8 with ggplot2 3.0.0, and trying to add and align text labels to my scatter plots. However, it seems the hjust parameter is being ignored by plotly in geom_text(aes(....), hjust = "left"). (Also tried hjust = 0.)
GGPLOT OUTPUT
See it renders fine in plot window as a ggplot with labels left aligned.
PLOTLY OUTPUT
But the alignment is lost in conversion, and the text is centered.
So the question, is fixing this alignment possible with plotly?
TEST EXAMPLE CODE
library(ggplot2)
library(data.table)
library(plotly)
data(mtcars)
plotdata <- as.data.table(mtcars)
plotdata$carname <- rownames(mtcars)
# take a small demo subset
plotdata <- plotdata[1:10,]
gg <- ggplot(plotdata, aes(x = wt, y = mpg, label = carname)) +
geom_point() + theme_minimal()
gg <- gg + geom_text(aes(label = carname),
size = 2,
hjust = "left")
print(gg)
# convert ggplot
p <- ggplotly(gg)
p
You just need to add text position textposition = "right":
ggplotly(p) %>% style(textposition = "right")
Output:
Ref: https://github.com/ropensci/plotly/issues/769

Can you vary text size within the same ggplot2 axis?

I'd like to use ggplot2 to make a plot where the axis text size varies between labels- for example, larger font every five ticks with smaller font for the intervening ticks. I looked at using minor_breaks in scale_x_continuous, but I couldn't find a way to label the minor breaks.
The best I've got to work so far is a modification from this answer where I use bquote to pass an expression for the axis labels:
label_span <- 1:40
ShrinkIf <- Vectorize(function(val) {
if (val %% 5 == 0) return(as.character(val))
return(bquote(scriptstyle(.(as.character(val)))))
})
x_labels <- ShrinkIf(label_span)
x_labels <- purrr::invoke(expression, x_labels)
ggplot(mtcars, aes(x = mpg , y = hp)) +
geom_point() +
scale_x_continuous(breaks = label_span, labels = x_labels)
Is there a better way to go about this, or maybe a way with a little more control of the label size (or even font choice / text decoration, etc)? Thanks in advance for your help!
You can create a vector of text sizes and add it using element_text()
library(ggplot2)
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point() +
theme(
axis.text.x = element_text(size = rep(c(12,24), 3))
)

`showticklabels = FALSE` in `plotly::layout()` fails with multiple subplots

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)

how to show all data points on y axis in ggplot heatmaps?

I've created a heatmap using ggplot
library(plyr)
library(scales)
guide_ind <- ddply(guide_tag[company == FALSE], .(tag), transform, rescale = rescale(count))
(p <- ggplotly(ggplot(guide_ind, aes(tag, username)) +
geom_tile(aes(fill = rescale),colour = "white") +
scale_fill_gradient(low = "white",high = "steelblue") +
theme(axis.text.x = element_text(angle= 90, hjust=1), legend.position= "bottom") )
)
I have about 700 rows of user name, and I would like to make sure that all the usernames are visible in document so that when I produce this in markdown, it will show the names individually instead of overlapping like the picture below.
I've tried using the fig.height, and gplot heatmap, but neither has worked.
Does anyone have suggestions to how to make all data points visible on the yaxis?

Labels outside of plots

I am hoping to add the labels "a)" and "b)" to my two plots so that I can differentiate and discuss them more effectively when writing up. Ive tried to do this through the text and legend functions but I'm not getting any good results. Ideally I would have the a) in the very top left of the ep.var.hist plot (1st plot) and the b) in the very top left of the tp.var.hist plot (2nd plot), with the labels sitting outside the actual plot and above the y-axis labels.
My code is below
par(mfrow=c(2,1), mar=c(4,4,0.9,4))
ep.var.hist<-hist(data.ep, breaks=5, xlim=c(0,0.011), ylim=c(0,6000), xlab=NULL, main=NULL)
tp.var.hist<-hist(data.tp, breaks=66, xlim=c(0,0.011), ylim=c(0,6000), xlab="Variance", main=NULL)
Working with the cowplot package, designed to ease the process for producing publication ready plots.
library(cowplot)
library(ggplot2)
sepal <- ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
geom_bar(stat = "identity") +
theme(text = element_text(margin = margin(), debug = FALSE))
petal <- ggplot(data = iris, aes(x = Species, y = Petal.Length)) +
geom_bar(stat = "identity") +
theme(text = element_text(margin = margin(), debug = FALSE))
plot_grid(sepal, petal, labels = c("A", "B"))
plot_grid and save_plot (a polished version of ggsave) are my two favorite cowplot functions. I highly recommend looking at the help pages for more options and customization.
If you really want to keep it to just graphics try this solution, I think you are looking for adj = 0:
par(mfrow=c(2,1), mar=c(4,4,0.9,4))
petal <- hist(iris$Petal.Length, main = "Petal", adj = 0)
sepal <- hist(iris$Sepal.Length, main = "Sepal", adj = 0)
Full disclaimer I would strongly consider using ggplot2 in the long term like #rosscova suggested. You will have a lot more options for controlling the details of your plots, plus lots of modern visualizations that base R just can't do. There is a reason why ggplot2 is so popular :)
EDIT: I apologise, I went ahead and answered without realising you said "outside of plots", which I don't think my answer can achieve.
I don't know how to do this in base, but ggplot2 has the annotate function to achieve what you're after. Here's an example from which you can start playing (I've added a few bits you might want to help get you started):
library( ggplot2 )
plot <- ggplot( diamonds ) +
geom_histogram( aes( carat ), bins = 30 ) +
annotate( "text", label = "label here", x = 1, y = 7500, col = "red" ) +
annotate( "text", label = "and another", x = 2, y = 5500, col = "blue" )
plot <- plot +
xlim( 0, 3 ) +
ggtitle( "Main title" ) +
xlab( "label x" ) +
ylab( "label y" )
plot
Adjust the x and y values within the annotate function to move the label around. You can add as many of these as you like by adding more calls to annotate.

Resources