Plotly does not properly show axis numbers with math_format - ggplot2

The code below indicates that, while using math_format command in ggplot 'labels', the plot displays well if ggplot is used, but it fails if it is displayed through plotly. I need to use plotly in my code. Does somebody have some suggestion?
library(tidyverse)
library(scales)
library(plotly)
p <- mtcars %>% ggplot(aes(x=mpg, y=disp))+
geom_point() +
scale_x_continuous(trans = log_trans(),
breaks = trans_breaks("log", function(x) exp(x), n.breaks = 5),
labels = trans_format("log", math_format(e^.x, format = function(x) number(x, accuracy = 0.01, decimal.mark = ','))))
p
ggplotly(p)

Related

Bar plot in ggplot2

Will anybody help develop a bar plot using ggplot2 for this data:
I am unable to develop a barplot for this data.
In ggplot you should make your data in a longer format. For this you could use pivot_longer from tidyr. With that you could create a stacked barplot like this:
df <- data.frame(Valley = c("Hushey", "Kanday", "Thallay"),
Female = c(144, 43, 45),
Young = c(160, 43, 22),
Yearling = c(162, 20, 25))
library(tidyr)
library(dplyr)
library(ggplot2)
df %>%
pivot_longer(cols = -Valley) %>%
ggplot(aes(x = Valley, y = value, fill = name)) +
geom_col()
Or make a facet plot using facet_wrap like this:
df %>%
pivot_longer(cols = -Valley) %>%
ggplot(aes(x = name, y = value)) +
geom_col() +
facet_wrap(~Valley)
Created on 2023-01-23 with reprex v2.0.2

Legend title for ggplot with several options

I am plotting density plots by groups with different colors and linetypes.
ggplot(data = data,
aes(x=X, group=G, color=factor(G), linetype = factor(G))) +
geom_density()
How can I change the legend title while keeping it as one legend?
Your issue comes from the fact that when you add a legend title (e.g., using scale_color_discrete), you're only doing it for color and not linetype. The first plot is fine because the legends have identical arguments (i.e., neither is specified). You need to provide identical specifications for each legend in order to combine them. See this post for more information.
There may be other ways around this issue, but we can't say for certain since we can't access your dataset (data).
library(tidyverse)
data(mtcars)
# this is ok; one legend
ggplot(data = mtcars,
aes(x=mpg, group=cyl, color=factor(cyl), linetype = factor(cyl))) +
geom_density()
# this is not ok; now two legends
ggplot(data = mtcars,
aes(x=mpg, group=cyl, color=factor(cyl), linetype = factor(cyl))) +
geom_density() + scale_color_discrete("New Name?")
# this is ok again; back to one legend
ggplot(data = mtcars,
aes(x=mpg, group=cyl, color=factor(cyl), linetype = factor(cyl))) +
geom_density() +
scale_colour_manual(name = "New!",
labels = c("4", "6", "8"),
values = c("red", "blue", "pink")) +
scale_linetype_manual(name = "New!",
labels = c("4", "6", "8"),
values = c(2, 4, 6))

ggplot2 geom_text position in pie chart

I am plotting pie charts with ggplot2 and succeeded in having the percentage-labels centered in each slice
library(dplyr)
library(ggplot2)
library(ggpubr)
library("readxl")
df <- read_excel("Radiocomp.xlsx")
df$Pattern <- factor(cc$Pattern)
str(cc)
GGO <- ggplot(data=df, aes(x = "", y = GGO, fill = Pattern)) +
geom_bar(stat="identity", color = "white") +
geom_text(aes(label = paste0(GGO, "%")), position = position_stack(vjust = 0.5)) +
coord_polar("y") +
theme_void()
GGO
Pie chart
I try to place the percent-label outside the pie for better readability
Any recommendation?
Thank you
This can be achieved by setting the x aesthetic inside geom_text, e.g. x = 1.6 will put the label just outside of the pie.
library(ggplot2)
library(dplyr)
# example data
mpg1 <- mpg %>%
count(class) %>%
mutate(pct = n / sum(n))
ggplot(mpg1, aes(x = "", y = pct, fill = class)) +
geom_bar(stat = "identity", color = "white") +
geom_text(aes(x = 1.6, label = scales::percent(pct, accuracy = .1)), position = position_stack(vjust = .5)) +
coord_polar("y") +
theme_void()
Created on 2020-06-03 by the reprex package (v0.3.0)

ggplot multiple densities with common density

I would like to plot something that is "between" a histogram and a density plot. Here is an example:
library(ggplot2)
set.seed(1)
f1 <- rep(1, 100)
v1 <- rnorm(100)
df1 <- data.frame(f1, v1)
f1 <- rep(2, 10)
v1 <- (rnorm(10)+1*2)
df2 <- data.frame(f1, v1)
df <- rbind(df1, df2)
df$f1 <- as.factor(df$f1)
ggplot(df, aes(x = v1, colour = f1)) +
geom_density(position="identity", alpha = 0.6, fill = NA, size = 1)
You will see that the area under each curve is 1.0, which is OK for a density. BUT notice that the second distribution is made up of just 10 observations rather than the 100 of the first. What I would like is that the area under curve 2 reflects this, e.g. is a tenth of that of curve 1. Thanks.
There is a computed variable for stat_density that you can use, called count.
ggplot(df, aes(x = v1, colour = f1)) +
geom_density(position="identity", alpha = 0.6, fill = NA, size = 1,
aes(y = after_stat(count)))
Note for ggplot2 <3.3.0 use stat(count) instead of after_stat(count).
You can find these tricks in the documentation of ?geom_density() under the section "Computed Variables".

How to adjust plot areas in ggplot?

I am trying to use grid.arrange to display multiple graphs on the same page generated by ggplot.Each subplot with difference x and y scale. Two subplot share the legend. My perpose is that to display the plot areas same size. Are there parameter to adjust plot area ( except legend area)? The facet is inadequate to arrange it.
df <- data.frame(class=paste0('a',1:20),
x1=runif(20),
x2=runif(20),
y1=runif(20),
y2=runif(20))
p1 <- ggplot(df,aes(x=x1,y=y1))+
geom_point(aes(color=class),size=2,show.legend=TRUE)+
stat_smooth(method='lm',color='black')+
theme_bw()
p2 <- ggplot(df,aes(x=x2,y=y2))+
geom_point(aes(color=class),size=2,show.legend=FALSE)+
stat_smooth(method='lm',color='black')+
theme_bw()
grid.arrange(p1,p2,nrow=2)
Using patchwork package
# install.packages("devtools", dependencies = TRUE)
# devtools::install_github("thomasp85/patchwork")
library(patchwork)
p1 / p2 + plot_annotation(title = "Plot title",
subtitle = "Plot subtitle",
tag_levels = 'A',
tag_suffix = ')')
Created on 2018-11-20 by the reprex package (v0.2.1.9000)