Questions tagged [ggplot2]

ggplot2 is an actively maintained open-source chart-drawing package for R, written by Hadley Wickham, based upon the principles of "Grammar of Graphics". It partially replaces R's basic plot and the lattice package, while providing a clean, powerful, orthogonal and fun API.

1
vote
0answers
10 views

Align ggplot legend automatically to width of a plot in R

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) #...
1
vote
0answers
16 views

Graph differences between groups in ggplot2

For an example dataframe: df1 <- structure(list(name = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b"...
-1
votes
0answers
4 views

how can I describe the model ad and bpd?

how can I describe the model ad and bpd? for this model i was using: library(ISwR) library(GGally) ggpairs(secher)
0
votes
1answer
17 views

Lines in ggplot with segment function

I have the following script from the DTW package and I'm looking for the equivalent code of this example in ggplot framework: library(dtw) idx<-seq(0,6.28,len=100); query<-sin(idx)+runif(100)/...
0
votes
1answer
11 views

Using expand_limits in ggplot when counts are not a column in the dataset (R)

I have a dataset and I need to plot a bar chart of the counts of the different outcomes of a certain column. For this example I am using the mtcars dataset. When I first attempted this, I found that ...
0
votes
1answer
20 views

stat_compare_means anova on selected groups

I would like to compute an anova only including gr1-gr3 excluding gr4. Is this possible using stat_compare_means()? x value gr1 3.543 gr1 2.12 gr1 4.56 gr2 3.3 gr2 2.12 gr2 4.1 gr3 3.32 gr3 2.3 gr3 ...
1
vote
1answer
18 views

Controlling x-labels in facet_wrap ggplot2

I have a plot where I would like the labels for the first plot to be 2018, 19, 20, 21 and for the other two 18, 19, 20, 21. Is this possible? Example library(ggplot2) df <- structure(list(date = ...
0
votes
0answers
45 views

how to use ggplot function to plot

This is my ggplot function and the resultant error plot1 = ggplot(data= RiskReturn, aes(x = cov_matrix, y = averagereturn)) plot1 = plot1 + geom_point() plot1 = plot1 + xlab("Risk/Variance") + ylab(...
1
vote
0answers
20 views

color cells of facet_grid according to cluster [duplicate]

This code clusters the iris data's 4 dimensions: library(SOMbrero) library(dplyr) library(ggplot2) library(datasets) set.seed(42) rm(list = ls(all = TRUE)) setwd('C:/Data') #str(iris) x_dim = 6 ...
0
votes
0answers
5 views

How to set free facet_wrap y-axis range so that it caps the maximum data point?

I have one face_wrap plot (three subplots) and I am satisfied with all the elements except for one thing: the two y-axis in the middle and bottom plot they don't cap the maximum data point. I don't ...
0
votes
0answers
13 views

Plotting 2 way interaction from linear regression

I have a linear regression model where I have two categorical variables and control for baseline measure. For instance, DV2 <- runif(30, min=0, max=1) DV1 <- runif(30, min=0, max=1) gender <-...
1
vote
1answer
34 views

Adding points, symbols, and legends to ggplot

I have created a plot using ggplot (with DF1 dataset below). I would like two additions to this plot: to add symbol based on DF.SYMBOL dataset (on specified times for two IDs: different shape and ...
1
vote
1answer
17 views

ggplot legend with geom_rect

I am trying to create a legend item for a geom_rect legend. But the legends also shows a shape as well instead of a simple grey rectangle (see example below). Is there a way to create a legend item ...
0
votes
1answer
12 views

“Reversed” use of fct_infreq() in ggplot2

I used fct_infreq() to reorder mpg's model factor levels by frequency. However, the provided code results in a plot where the factor with least counts will appear at the top of the plot. I would like ...
0
votes
1answer
29 views

Facet_wrap: conditional formatting based on label_value?

For some of my charts I would like to change the formatting of the numbers on the x axis for each facet conditionally on the label of the facet e.g. based on the order of magnitude (e.g. k, M, B etc.)....
1
vote
1answer
25 views

Custom legend in ggplot2: how to fill without factors?

I am making a personality survey that will generate score reports for participants. I want to make these as easy to read and understand as possible, so I am generating a normal curve for the surveyed ...
-1
votes
2answers
24 views

Plot a data frame as a grouped bar plot

How best can I plot this dataset as a grouped bar plot as shown below? Zone Oct Nov Dec 1 554370.8 687899.0 702890.80 2 1072629.8 899767.6 1058627.17 3 660052.9 689939.9 ...
1
vote
0answers
19 views

Obtaining 4 barplots in a panel - error obtained running a previous stackoverflow example

I want to create a panel of 4 barplots using ggplot. This stack overflow post contains an example: Simple bar plot with multiple variables in R - Similar to Excel When I run the code, I obtain an ...
0
votes
1answer
21 views

Linetype for points in ggplot2

I'm trying to get a hollow point to have the same line type as an associated line range. For instance library(tidyverse) data.frame( x = letters[1:2] %>% factor, ymin = 4:5, y =...
2
votes
3answers
29 views

How to assign colors using a variable containing color names in a grouped ggplot?

In this simple example, I create a variable with the names of colors. df <- mtcars %>% mutate(color ="green", color = replace(color, cyl==6, "blue"), color = replace(color, cyl==8, ...
0
votes
2answers
37 views

R barplot color by column while keeping the dataframe order

I am trying to create a bar plot in R that will keep the order of my data and that will color the data by a different color. my data looks like that: row.name BMP2 type qaz 4 gf ...
1
vote
1answer
21 views

Plot ribbon between lines created with geom_abline

I am trying to create a shaded area between lines created with geom_abline require(ggplot2) val_intcpt <- c(-1,1) ggplot() + geom_point(data = iris, mapping = aes(x = Petal.Length, y = Sepal....
4
votes
1answer
41 views

Use ampersand in ggplot facet strip label

How can I use an ampersand in a facet strip label? I have tried as is, and using unicode with the same result. MWE library(ggplot2) mtcars$am2 <- factor(mtcars$am, labels=c("N[one]==1 \U0026 N[...
3
votes
1answer
41 views

How to get x axis text to have a vertical orientation (NOT rotated with angle=90/270)

I want my X axis text to look like: J a n not be rotated with the letters turned. I want to keep it as a date axis. I know I could make it discrete with values of "J\na\na\n" for instance. ...
3
votes
2answers
47 views

selective jitter of geom_points

I have a ggplot where some of the points are overlapping with a few others. I was wondering if there is a way to put the points one above the other. In my case, there are 2 points at most overlapping. ...
1
vote
3answers
32 views

Reordering categories in stacked bar chart based on count

I want to produce a stacked bar chart in ggplot2 where the bars in the stack are ordered according to the count of that category. When I attempt this using the below code, it appears that ggplot2 ...
0
votes
1answer
31 views

R: Two graphs (boxplot and barplot) sharing one X-Axis

I am trying to match two graphs in such a way that the two graphs are located vertically above each other sharing one x Axis I already tried to use ggplot but didn't succeed. I did not manage to ...
0
votes
2answers
25 views

Change the background color into three areas when we have MANY plots ggplot2 R

I have the following code which produces the plot below: ggplot(data = factor_output, aes(y=F.Difference, x=reorder(Action.Title, F.Difference), fill=Efficacy.Median)) + coord_flip() + ...
0
votes
1answer
19 views

gganimate renders pixelated in windows 10

I must render some animations using gganimate but the images are not good looking. They lack definition and borders are pixelated. Is there any way of getting better results in windows 10? My code is:...
0
votes
0answers
38 views

Remove NA values for bar plot in ggplot2.

The issue is pretty straightforward. I am trying to generate a bar plot containing GDP per capita for several countries. The data is incomplete as some of the values are missing. For instance, for ...
2
votes
2answers
37 views

Improper positioning when plotting adjacency matrix - R ggplot

I want to plot the adjacency matrix of a graph like a chessboard (black for 1s, white for 0s, or viceversa) [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 1 0 0 0 0 [...
0
votes
0answers
32 views

Real-time auto incrementing ggplot in R with timestamp

Continuing on this thread : Real-time auto incrementing ggplot in R library(animation) library(ggplot2) # your data n <- 200 df <- data.frame(time=1:n,y=runif(n)) window <- 100 # create ...
0
votes
2answers
38 views

Draw circles around points belonging to a factor level in ggplot

A previous post describes how to draw red circles around points which exceed a given value in ggplot. I would like to do the same for anomaly detection results, but instead have the circles drawn ...
0
votes
1answer
31 views

How do I calculate the proportion of one value in a variable to use in a line graph?

I am working with a Supreme Court dataset. I am trying to visualize the percentage of cases in each term that were decided by a one-vote margin. I have variables for terms, the number of votes in the ...
0
votes
1answer
25 views

Labeling points using qplot in R

I'm having trouble labeling points in R. I've created a qplot that uses four numeric variables I'm plotting as the x and y axes, the color of the points and the size of the points. When I try to add ...
0
votes
2answers
48 views

Scatter plot with small pie charts with R

I have this data below called test1.melted. I also have the code to plot my data using package scatterpie, but due to inherent problem of scatterpie (if coordinates are not cartesian,i.e. equal ...
3
votes
1answer
28 views

ggplot2: display every nth value on discrete axis

How I can automate displaying only 1 in every n values on a discrete axis? I can get every other value like this: library(ggplot2) my_breaks <- function(x, n = 2) { return(x[c(TRUE, rep(FALSE, ...
2
votes
1answer
26 views

How to change the transparency of a continuous scale legend when calling the alpha argument in a geom in ggplot2?

I need the alpha for the legend of the continuous scale colourbar to match that of the call in the geom. mpg %>% ggplot(aes(x = displ, y = cty)) + geom_point(aes(colour = hwy), alpha = 0.33)
0
votes
0answers
38 views

How to properly use scale_x_continuous with time intervals

I am trying to build a 24 hours long time interval, by scale_x_continuous: ggplot() + scale_x_continuous(breaks = seq(0, 24, by = 1)) + geom_histogram(aes(x=df$h1), fill="red", color="red", alpha = ...
1
vote
1answer
22 views

adjusting position of text above an error bar in ggplot

I have the following data frame: df <- structure(list(Gender = c("M", "M", "M", "M", "F", "F", "F", "F"), HGGroup = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("Low: \n F: <11.5, ...
0
votes
0answers
26 views

How to put small pie chart for distribution of different variables within geom_point?

I have this dataframe called test1.melted. The first column contains diluted samples (and their proportion) which as a whole makes each geom_point in the plot for which I have the code below. I want ...
2
votes
0answers
28 views

Facet function in highcharts

I have a data like this example: df <- data.frame( date = seq(as.Date("2015-01-01"), as.Date("2015-12-31"), 100), id = rep(LETTERS, each = 4)[1:100], replicate(2, sample(1001, 100)) ...
3
votes
1answer
27 views

Multiple Layers in ggplot2

I want to overlay a plot of an empirical cdf with a cdf of a normal distribution. I can only get the code to work without using ggplot. rnd_nv1 <- rnorm(1000, 1.5, 0.5) plot(ecdf(rnd_nv1)) lines(...
2
votes
2answers
51 views

merge color and shape in legend for two variables in ggplot2

Here's my data: delta <- structure(list(rcp = c("rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "...
1
vote
0answers
56 views

Swimmer survival plot ggplot_Events color coded by time duration

One question regarding the time axis for Swimmer waterfall plot. I generated the swimmer plot using the code below> However, I would like to have the bars for each subject colored by the ...
0
votes
1answer
23 views

Can I change the color of the surv.median.line in my Kaplan Meier plot using R ggsurvplot?

I use the script below to plot a Kaplan-Meier curve. I think that the median survival line is a great tool. However, the median survival line is drawn as a dashed black line, which is graphically ...
0
votes
0answers
34 views

Why does my ggplot have the wrong y-axis scale?

I'm using a concatenated version of frankbi's Price of weed data (https://github.com/frankbi/price-of-weed for the original, https://github.com/Travis-Barton/Github_code for the cleaned version). I ...
1
vote
1answer
35 views

Visualising big set of points with third feature as a color - a way to improve a speed

I have a pretty big dataset (around 5e5 rows) of (x, y) coordinates with additional feature z. It's something like this: x <- rnorm(1e4, 0, 5) y <- rnorm(1e4, 0, 10) dist <- sqrt(x^2 + y^2) ...
0
votes
1answer
18 views

Add points to usmap with ggplot in r

I was able to create a US map with this tutorial. When I tried to add additional points to it they all ended up in South Dakota, no matter what I input for data. library(ggplot2) library(usmap) ...
0
votes
1answer
19 views

Modify point stroke on a PCA biplot using autoplot function

I am using the autoplot function to make a PCA biplot. In my case, I would like to increase the point stroke to improve the readability of the plot. How can I do that? Here's an example: library(...