I'm a little confused as to why ggplot can render geom_area correctly in the first plot, but when wrapped with ggplotly it produces something a little different. Any insights?
library(tidyverse)
library(plotly)
my_dates <- Sys.Date() + c(1,2,3,4,4,5,6)
my_data <- tibble(a = my_dates,
b = c(1,3,7,10,14,16,22))
my_plot <- ggplot(my_data, aes(a, b)) +
geom_line() +
geom_area(position = "identity", alpha = 0.6)
my_plot
ggplotly(my_plot)
Created on 2020-04-03 by the reprex package (v0.3.0)
Related
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)
Is it possible to plot with ggplot2 3D surface which is presented as (x, y, z)-vector with labeled countour lines?
Desired result is presented below
Surface map with countour lines
This is exactly what the geomtextpath package was built for.
Example copied from ?geomtextpath::geom_textcontour
library(geomtextpath)
#> Loading required package: ggplot2
df <- expand.grid(x = seq(nrow(volcano)), y = seq(ncol(volcano)))
df$z <- as.vector(volcano)
ggplot(df, aes(x, y, z = z)) +
geom_contour_filled(bins = 6, alpha = 0.6) +
geom_textcontour(bins = 6, size = 2.5, padding = unit(0.05, "in")) +
scale_fill_manual(values = terrain.colors(11)) +
theme_classic() +
theme(legend.position = "none")
Created on 2023-01-26 with reprex v2.0.2
I have a question regarding markdown and patchwork. Let's say I combined figures FigureLength and FigureWidth to make CombinedFigure through patchwork (two figures on top of each other):
library(datasets)
library(patchwork)
data(iris)
FigureLength <- iris %>%
ggplot(aes(x = Species, y = Sepal.Length)) +
geom_boxplot()
FigureWidth <- iris %>%
ggplot(aes(x = Species, y = Sepal.Width)) +
geom_boxplot()
CombinedFigure <- FigureLength / FigureWidth
Both figures nicely on top of each other in R
save(CombinedFigure, file = here::here ("CombinedFigure"))
In Markdown when I put the following only Sepal Length appears in my word document and not both figures on top of each other.
```{r CombinedFigure, dpi = 300, echo = FALSE}
CombinedFigure
```
What appears in markdown - only one figure
Thanks for your help.
When I run the following code:
---
title: "Untitled"
author: "Author"
date: "4/4/2022"
output: word_document
---
```{r}
library(datasets)
library(tidyverse)
library(patchwork)
data(iris)
FigureLength <- iris %>%
ggplot(aes(x = Species, y = Sepal.Length)) +
geom_boxplot()
FigureWidth <- iris %>%
ggplot(aes(x = Species, y = Sepal.Width)) +
geom_boxplot()
CombinedFigure <- FigureLength / FigureWidth
```
```{r CombinedFigure, dpi = 300, echo = FALSE}
CombinedFigure
```
I to get both figures nicely on top of each other in WORD like this:
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)
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)