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
Related
I am arranging multiple plot using the patchwork package. One of the plots has the text arranged vertically, which pushes the x-axis label down (as it should), but when I combine with a second plot, the x-axis labels in both plots are moved down. I would like to keep the x-axis label of the second plot in its original position. Easier to explain with an example:
library(ggplot2)
library(patchwork)
# Toy data
mtcars2 <- mtcars[1:5, ]
mtcars2$mod <- row.names(mtcars2)
# make 2 plots
p1 <- ggplot(mtcars2, aes(mod, mpg)) +
geom_col() +
theme(axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1))
p2 <- ggplot(mtcars2, aes(hp, disp)) +
geom_point()
# arrange plots next to each other
p1 + p2
But I want:
Is this possible? I'm not tied to patchwork and I tried gridExtra::grid.arrange() but that resized the plots instead.
Using library(cowplot), x axis of p2 will go down, not like your example, but i wish it help you. If you need to let location of x - axis in same position, please let me know.
Additional note from #phalteman
By adding , align = "h", axis = "b" in plot_grid, it really becomes what you wanted!!
library(cowplot)
mtcars2 <- mtcars[1:5, ]
mtcars2$mod <- row.names(mtcars2)
# make 2 plots
p1 <- ggplot(mtcars2, aes(mod, mpg)) +
geom_col() +
theme(axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1))
p2 <- ggplot(mtcars2, aes(hp, disp)) +
geom_point()
# arrange plots next to each other
plot_grid(p1, p2, align = "h", axis = "b") #Thanks to #phalteman
I'm using the package patchwork to combine multiple ggplot2 plots vertically. I'd like the scales for each plot to be directly above one another, regardless of the length of the scale name. At the moment, the scales are not aligned above one another.
I'm open to using ggpubr or facet_grid() if they would make it possible, but I've seen that facets doesn't allow multiple scales, and I haven't found any solution using ggpubr
library(ggplot2)
library(patchwork)
set.seed(0)
testdata <- data.frame(x=1:10, y=1:10, col=runif(10))
g1 <- ggplot(testdata, aes(x=x,y=y,col=col)) + geom_point() +
scale_color_gradient(name="Short")
g2 <- ggplot(testdata, aes(x=x,y=y,col=col)) + geom_point() +
scale_color_gradient(name="A rather longer name")
g1/g2
ggsave("testfile.tiff", units = "mm", device="tiff",
width=100, height=100, dpi = 100)
Ideal output:
With plot_layout you can "collect" the legends. This uses as default theme(legend.position = 'right'). You can add this after plot_layout with & theme(legend.position = 'right') and adjust the position if you want to change the location of the legends.
g1/g2 + plot_layout(guides = 'collect') # & theme(legend.position = 'right') <- adjust position here!
ggsave("testfile.tiff", units = "mm", device="tiff",
width=100, height=100, dpi = 100)
I'd also be curious to learn of a patchwork parameter than could fix this, but I don't think there is one (please correct me if I'm wrong). You may have noticed that Hadley's answer is more than 10 years old and people have been working on ggplot2 since then. The ggnewscale package solves the problem of having multiple scales per plot. Here is a facetted approach using multiple colourscales:
library(ggplot2)
library(ggnewscale)
set.seed(0)
testdata <- data.frame(x=1:10, y=1:10, col=runif(10))
ggplot(mapping = aes(x = x, y, y)) +
geom_point(data = transform(testdata,
facet = factor("Top", c("Top", "Bottom"))),
aes(colour = col)) +
scale_colour_continuous(name = "Short") +
new_scale_colour() +
geom_point(data = transform(testdata,
facet = factor("Bottom", c("Top", "Bottom"))),
aes(colour = col)) +
scale_colour_continuous(name = "A rather longer name") +
facet_wrap(~ facet, ncol = 1)
I am using ggplot in R to generate a heatmap. I would like to align the width of the legend in "bottom" position to the width of the ggplot itself.
Example code:
# Load packages
library(tidyverse)
# Create dataframe
df <- expand.grid(x = seq(1,100), y = seq(1,100))
# Add variable
set.seed(123)
df$z <- rnorm(nrow(df))
# Generate plot
ggplot(data = df, aes(x = x, y = y)) +
geom_tile(aes(fill = z)) +
theme(legend.position = "bottom")
With the option legend.key.width = unit(2, "cm") I was able to set the width manually. However, I am wondering if there is a way to automatically align the width of the legend to the width of the plot?
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
I'm facing this problem with ggplot2 and plotly packages with R.
I'm creating a stacked bar chart, and, first of all, my data:
# here my data
family <- c('one','one','one','one','one','one','one','one',
'two','two','two','two','two','two','two','two')
group <- c('alpha','beta','alpha','beta','alpha','beta',
'alpha','beta','alpha','beta','alpha','beta',
'alpha','beta','alpha','beta')
field <- c('a','a','b','b','c','c','d',
'd','a','a','b','b','c','c','d','d')
value <- sample(1:10, 16, replace =TRUE)
data <- data.frame(family, group, field, value)
remove(family,group,field,value)
Then, I created my stacked barchart with ggplot2:
# here the plot
library(ggplot2)
library(plotly)
p <- ggplot(data, aes(x = group, y = value, fill =reorder(field,value)))
p <- p + geom_bar(position = "fill",stat = "identity")
p <- p + ggtitle("A simple example")
p <- p + theme(axis.text.x = element_text(angle = 90, hjust = 1))+ guides(fill=FALSE)
p <- p + facet_grid(family~.)
Lastly, I put it as a plotly output:
ggplotly(p)
remove(data)
The result is good:
My issue is: if you choose one of the occurrences on the legend you have this (choosing d):
I really would like that if you choose for example the d, the plot "returns" to a 100% stacked barplot, recalculating the percentage logically. Is it possible? If so, how?
Thanks in advance.