ggplot2

How to zoom in on a specific range of values for a categorical variable in ggplot2?

最后都变了- 提交于 2021-02-20 02:16:06
问题 I just want to zoom in on the x-axis between the values ford and nissan in the mpg dataframe. Package used: tidyverse But I am getting the following error when using the coord_cartesian() function: p<-ggplot(mpg,aes(x=manufacturer,y=class)) p+geom_point()+ + coord_cartesian(xlim = c('ford','nissan')) Error in +coord_cartesian(xlim = c("ford", "nissan")) : invalid argument to unary operator 回答1: You can use a function for contextual zoom from ggforce package ( facet_zoom ) to achieve this: #

Creating multiple ggplots with dplyr

China☆狼群 提交于 2021-02-19 23:08:14
问题 After creating a plot matrix using GGally::ggpairs() , I would like to store the individual scatter plots for later use. Here is my current code: # load necessary package library(GGally) # loads `ggplot2` library(magrittr) # allows for the use of `%>%` # create a matrix of plots mtcars %>% na.omit() %>% ggpairs(columns = 1:7) # how do I automate this process? P1 <- ggplot(aes(x = disp, y = hp)) + geom_point() P2 <- ggplot(aes(x = drat, y = hp)) + geom_point() P3 <- ggplot(aes(x = hp, y = qsec

Creating multiple ggplots with dplyr

别来无恙 提交于 2021-02-19 23:07:01
问题 After creating a plot matrix using GGally::ggpairs() , I would like to store the individual scatter plots for later use. Here is my current code: # load necessary package library(GGally) # loads `ggplot2` library(magrittr) # allows for the use of `%>%` # create a matrix of plots mtcars %>% na.omit() %>% ggpairs(columns = 1:7) # how do I automate this process? P1 <- ggplot(aes(x = disp, y = hp)) + geom_point() P2 <- ggplot(aes(x = drat, y = hp)) + geom_point() P3 <- ggplot(aes(x = hp, y = qsec

Convert English numbers to Persian for ggplot

99封情书 提交于 2021-02-19 08:32:30
问题 I am working on a data visualization project using ggplot . I have my original data in English: world_ecommerce <- data.frame( year = factor(c(2014, 2015, 2016, 2017, 2018)), score = c(1336, 1548, 1845, 2304, 2842) ) I want to visualize it as a bar chart and show all numbers in persian. My original Bar chart is: ggplot( world_ecommerce, aes(x = year, y = score, label = score), fill = "#56c7da" ) + geom_bar( stat = "identity", fill = "#56c7da", position = position_dodge(), width = 0.5, size =

ggplot add Normal Distribution while using `facet_wrap` [duplicate]

☆樱花仙子☆ 提交于 2021-02-19 07:53:06
问题 This question already has answers here : using stat_function and facet_wrap together in ggplot2 in R (6 answers) Closed 21 days ago . I'm looking to plot the following histograms: library(palmerpenguins) library(tidyverse) penguins %>% ggplot(aes(x=bill_length_mm, fill = species)) + geom_histogram() + facet_wrap(~species) For each histogram, I would like to add a Normal Distribution to each histogram with each species mean and standard deviation. Of course I'm aware that I could compute the

ggplot add Normal Distribution while using `facet_wrap` [duplicate]

谁说胖子不能爱 提交于 2021-02-19 07:52:51
问题 This question already has answers here : using stat_function and facet_wrap together in ggplot2 in R (6 answers) Closed 21 days ago . I'm looking to plot the following histograms: library(palmerpenguins) library(tidyverse) penguins %>% ggplot(aes(x=bill_length_mm, fill = species)) + geom_histogram() + facet_wrap(~species) For each histogram, I would like to add a Normal Distribution to each histogram with each species mean and standard deviation. Of course I'm aware that I could compute the

plotting multiple geom-vline in a graph

怎甘沉沦 提交于 2021-02-19 07:46:06
问题 I am trying to plot two ´geom_vline()´ in a graph. The code below works fine for one vertical line: x=1:7 y=1:7 df1 = data.frame(x=x,y=y) vertical.lines <- c(2.5) ggplot(df1,aes(x=x, y=y)) + geom_line()+ geom_vline(aes(xintercept = vertical.lines)) However, when I add the second desired vertical line by changing vertical.lines <- c(2.5,4) , I get the error: ´Error: Aesthetics must be either length 1 or the same as the data (7): xintercept´ How do I fix that? 回答1: Just remove aes() when you

Differentiate lines in geom_dumbbel by colour using levels of a variable

天大地大妈咪最大 提交于 2021-02-19 07:44:12
问题 I am trying to differentiate the lines by a variable "Trial_type", which has two levels. One of the levels is retention and I want that line to be blue, and the other level, transfer I want it to be red. I hope this produces a legend to make the plot more understandable. I have the plot made just missing those key ingredients. library(ggplot2) library(ggalt) library(tidyverse) library(dplyr) data10 <- structure(list(GROUP = c("RLR", "LRL", "RLR", "LRL", "RLR", "LRL", "RRR", "LLL", "RRR", "LLL

Find all local maxima of a geom_smooth curve in R ggplot?

回眸只為那壹抹淺笑 提交于 2021-02-19 07:40:52
问题 I need to find all local maxima of a geom_smooth() curve in R. This has been asked in Stack Overflow before: How can I get the peak and valleys of a geom_smooth line in ggplot2? But the answer related to finding a single maximum. What if there are multiple local maxima we want to find? Here's some sample data: library(tidyverse) set.seed(404) df <- data.frame(x = seq(0,4*pi,length.out=1000), y = sin(seq(0,4*pi,length.out=1000))+rnorm(100,0,1)) df %>% ggplot(aes(x=x,y=y)) + geom_point() + geom

Find all local maxima of a geom_smooth curve in R ggplot?

徘徊边缘 提交于 2021-02-19 07:39:49
问题 I need to find all local maxima of a geom_smooth() curve in R. This has been asked in Stack Overflow before: How can I get the peak and valleys of a geom_smooth line in ggplot2? But the answer related to finding a single maximum. What if there are multiple local maxima we want to find? Here's some sample data: library(tidyverse) set.seed(404) df <- data.frame(x = seq(0,4*pi,length.out=1000), y = sin(seq(0,4*pi,length.out=1000))+rnorm(100,0,1)) df %>% ggplot(aes(x=x,y=y)) + geom_point() + geom