# Questions tagged [ggplot2]

14372 questions

1

votes

2

answer

42

Views

### R- Unsure of what to set y value equal to to create plot with facets

I am trying to create a plot with a facet for each region. I added the region column at the end because I thought I could use that as the parameter for facet_wrap().
Also, what do I set y equal to if I want it to be equal to the values listed in the tibble below?
mydata

1

votes

2

answer

4.2k

Views

### ggplot2 : How to reduce the width AND the space between bars with geom_bar

I understand that one can change the width of a bar in geom_bar using the width argument. That does work, but then it creates a larger gap between the bars. Is there a way to manually push the bars closer to together? Should I be manipulating the axis somehow instead?
Here is an example, changing wi...

1

votes

1

answer

391

Views

### how to move tick marks and labels at right/left end of tiles in geom_tile ggplot?

I am unable to move the x-axis labels, including tick marsk, in a geom_tile to the right end of each tile, I would also like to add the zero at the left end. I have tried breaks and labels but with no luck. Also using breaks and labels doesn't really work.
I am trying to achieve what is done in this...

1

votes

2

answer

760

Views

### ggplot: Plotting timeseries data with missing values

I have been trying to plot a graph between two columns from a data frame which I had created. The data values stored in the first column is daily time data named 'Time'(format- YYYY-MM-DD) and the second column contains precipitation magnitude, which is a numeric value named 'data1'.
This data is ta...

1

votes

2

answer

305

Views

### creating custom annotations in only one facet of a ggplot

Is there a way to specify that a custom_annotation only applies to one facet of a ggplot?
For example, if I run the following code
library(tidyverse)
library(grid)
text_grob=grobTree(textGrob('text',x=0.5, y=0.6, rot=90,
gp=gpar(col='red')))
ggplot(mtcars, aes(x=mpg, y =drat))+
geom_point() +
facet...

1

votes

2

answer

264

Views

### Vertical equivalent of position_dodge for geom_point on categorical scale

I would like to dodge overlapping geom_point's vertically, when I have a categorical y variable.
library(tidyverse)
# all possible points
df %as.tbl
# randomly missing and overlapping points
# every green point has a pink point underneath, and every blue point
# has a green point underneath it.
see...

1

votes

2

answer

67

Views

### Why isn't my barplot rearranging properly when faceting with ggplot?

So I have made this barplot with this code, bars organised in descending order, great!
na.omit(insect_tally_native_ranges)%>%
group_by(native_ranges)%>%
dplyr::summarise(freq=sum(n))%>%
ggplot(aes(x=reorder(native_ranges,freq),y=freq))+
geom_col(color='#CD4F39',fill='#CD4F39',alpha=0.8)+
coord_flip(...

1

votes

2

answer

112

Views

### Plotting a ggplot2 graph in a power point slide with a given size - R

I'm trying to plot a ggplot2 graph in a power point slide with the officer package. I can do it actually (printing the ggplot2 directly in the ppt), but as I need to increase the size of the ggplot2 graph (for the ppt slide), and I have understood that ggplot2 graphs are dependent on the size of the...

1

votes

1

answer

52

Views

### Show alternate categorical labels in axis in ggplot2

I know there are some questions issuing this problem in this forum, as this one. However, my code is a little bit more complicated by using the package forcats.
To make my example reproducible, let's see two related datasets:
comb

1

votes

1

answer

42

Views

### ggplot match color code column to another column in R dataframe [closed]

I have this simple dataframe in the form:
x y tissue color_code
1 2 nerve #EEEE00
2 3 brain #33CCCC
There are several thousands rows for each tissue. Each tissue will have the same color_code (e.g. all rows with nerve have a color code of #EEEE00,etc.)
I would like to m...

1

votes

1

answer

33

Views

### Recreating a plot from Fundamentals of Data Visualization [closed]

I'd like to recreate this plot:Fundamentals of Data Visualization. See also below with the screenshot. I have searched the GitHub page link but I cannot find it. Can someone create a plot like a book with many distributions along the horizontal axis?
The plot from the book:

1

votes

1

answer

68

Views

### align labels to boxplot in ggplot

I have multiple boxplots which I wish to label with the actual values above. However, I can't seem to get them to align. I end up with something like the following where the labels are not aligned.
I have tried tinkering around with
geom_text(aes(label=value), position=position_dodge(width=0.9), v...

1

votes

2

answer

50

Views

### Unable to loop through ggplot histogram

I'm trying to loop through every column of the iris data set and plot a histogram in ggplot. So I'm expecting 5 different histograms to appear. However, my for loop below returns nothing. How can I fix this?
library(ggplot2)
for (i in colnames(iris)){
ggplot(iris, aes(x = i))+
geom_histogram()
}

1

votes

3

answer

40

Views

### ggplot geom_rect() error “object not found”

I'm trying to plot a geom_rect(). Why do I receive an Error in FUN(X[[i]], ...) : object 'Month' not found? If I run df$Month in my console the object is there:
df$Month
#> [1] 2019-01 2019-02 2019-03
#> Levels: 2019-01 2019-02 2019-03
Here's my code block:
library(tidyverse)
df Error in FUN(X[[i]]...

1

votes

1

answer

35

Views

### Could someone explain the difference between ggplot and geom_point?

I'm plotting the same data 2 separate ways, and I thought the resulting figures would be the same because they both make use of geom_point; however, they are quite different. Does anyone know what differs between these sets of commands? In both cases, I am trying to plot data with three color classe...

1

votes

3

answer

53

Views

### Ggplot2 - I can't insert the chart legend [duplicate]

This question already has an answer here:
Add legend to ggplot2 line plot
3 answers
Good evening everybody,
The data are:
Data X Y
01/01/16 17073 229
01/02/16 16094 375
01/03/16 17380 880
01/04/16 19993 9978
01/05/16 26290 24782
01/06/16 32982 36437
01/07/16 38490 42547
01/08/16 36688 439...

1

votes

1

answer

89

Views

### How to make a gradient colour in strip.background in ggplot?

I try to create a colour gradient in a fill arguments in the strip.background() function of ggplot2() package.
Here a picture of what I would like to have:
And the code I use to try to have it:
# Charge random data
data('mtcars')
# Create fake variable in order to create title into coloured box
mtca...

1

votes

1

answer

16

Views

### How to combine text in parse function with other (unparsed) text for plot annotation?

I am trying to add a simple annotation to a plot that reads: 'R^2: 0.90', where the 2 is shown as an exponent. I am having issues because the parse function removes the trailing 0 from my R-squared value, but I can't figure out how to paste together a parsed string and an unparsed string.
R2

1

votes

1

answer

30

Views

### geom_area produces blank areas between layers

I have the following data in data.frame tbl:
library(tibble)
tbl

1

votes

2

answer

26

Views

### Color issue with stacked area plot using ggplot2

I´m trying to create a stacked area plot with ggplot2.
So far it worked, but now I have problems with changing the colors of the area.
My plot looks like:
But I want it from green to red (middle should be in yellow tones). How can I change it?
ggplot(df.f, aes(x=Year,y=Population,fill=AgeClass)) +...

1

votes

1

answer

39

Views

### ggplot histogram color gradient

I am trying to visualize the diamonds data from ggplot2 in a histogram where I show the distribution of carats.
I've tried to move the aes values from ggplot to geom_histogram(), to try ..fill.., and to manipulate the code in different ways but the result is the same.
histogram

1

votes

1

answer

29

Views

### How to obtain a horizontal color gradient in ggplot2?

I want to create a horizontal thermometer chart, with a color gradient from green (left) to red (right).
I was able to add a color gradient but it's vertical instead of horizontal.
Secondly, is it possible to show the 'Me' text above the chart? It's hard to read when it's on top of the chart
library...

1

votes

3

answer

55

Views

### How to change ggplot2 x-axis values in barplot?

I would like to make a barplot in R, where the last bar in the graph indicates that last is the sum of all values whose the frequency is greater than a certain threshold. I want to represent this information on x-value correspondent to the last bar. For instance:
library(ggplot2)
x

1

votes

1

answer

23

Views

### Using ggplot2 to generate a barplot based on intervals of values

I have the following R data.table:
library(data.table)
dt1 = data.table(start = c(0, 3, 5, 7), end = c(3, 5, 7, 10), size = c(0, 3, 2, 1))
print(dt1)
start end size
1: 0 3 0
2: 3 5 3
3: 5 7 2
4: 7 10 1
I wanted to plot a barplot with ggplot2, whereby each interval...

1

votes

1

answer

30

Views

### Wrong legends in ggplot2 with two different plot types in one graph based on the same dataset

Please consider the following:
I want to plot a step-wise curve (using geom_step()) and some smooth lines (using geom_line()) in one graph using ggplot2.
I manage to create a graph but the labels are wrong and cannot be corrected using scale_color_discrete().
Desired outcome: Based on the data (see...

1

votes

1

answer

34

Views

### How do I add a variable to the legend without including it in the graph?

I would like to add a value to the legend without adding it to the plot.
library(tidyverse)
gear_color %
filter(gear != 4) %>%
ggplot(aes(vs, fill = gear)) +
geom_bar() +
facet_wrap(~gear) +
scale_fill_manual(values = gear_color)
How would I add Gear 4 to the legend without adding it to the plot?

1

votes

1

answer

31

Views

### Handling promises (rlang) with for loops and ggplot in R

The aim of this script was to replicate something like the figure below:
found on: https://robjhyndman.com/hyndsight/tscv/
The problem I have encountered relates to (I think) how R is handling my promises in ggplot.
Below is an example which reproduces my problem.
library(tidyverse)
process_starting...

1

votes

1

answer

38

Views

### ggplot2 for monthly time series data

I have a time series like this one from 1979 to 2018
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1979 15.414 16.175 16.342 15.447 13.857 12.530 10.311 8.041 7.051 8.748 10.943 13.336
1980 14.862 15.955 16.041 15.429 13.793 12.205 10.100 7.984 7.667 9.183 11...

0

votes

2

answer

17

Views

### How to make separate mean+errorbar on top of a scatter plot & how to make a horizontal mean line?

I want to make a position_dodged scatter plot with means represented as horizontal lines (with error bars).
I'm having trouble position_dodging the mean and error bars using stat_summary() and making the mean appear as a horizontal line.
I want to show that the mean is different between the sexes. I...

0

votes

1

answer

24

Views

### Visualise differences between factor levels using ggplot

I have a plot in my mind that I would like to create, but I don't know how to successfully achieve this goal.
I have 2 dataframes, one containing the mean value for each factor level, and the other, pairwise differences between these levels.
contrasts

0

votes

0

answer

8

Views

### Separate land and water using two gradients in ggplot

I'm trying to get two separate gradients for land and water on a map. I am able to get the water gradient (first figure), but not the land.
How can I set a grey gradient for land in this code (similar to figure 2)?
Code
library(marmap)
library(ggplot)
greys

-3

votes

0

answer

28

Views

### R: how to Plot the data according to categories in one plot

I have a data in excel file the data is like this
The Dat File Structure
Columns Names :
Algorithm , Combination , ArrivalRate , Timeout, Throughput
Data Look Like this :
Algorithm , Combination ,ArrivalRate, Timeout Throughput
Random, Identical, 1, 4, 6.746910832
Random, Identical,...

0

votes

0

answer

8

Views

### To log10 y-axis of stacked bar graph and adjust y-axis labels to match

Current code outputs a stacked bar graph of species at different sites. Im looking to log10 the y axis to provide a more concise view. And adjust y axis tick labels accordingly so it reads the log10 numbers.
I have already tried scale_y_log10 including different breaks and labels but have had limite...

6

votes

1

answer

43

Views

### Extending ggplot2 with a custom geometry for sf objects

I am trying to create a new geometry for ggplot as described here, while adapting it to deal with Simple Features objects.
As an example, let's take the same exercise of plotting the convex hull of a set of points. Thus, I wrote a new geom_envelope() function borrowing elements from geom_sf() and a...

1

votes

1

answer

1.9k

Views

### R ggplot geom_bar() label bars (with 'count')

I have a ggplot like this:
ggplot(df,aes(x=DateDiff, fill=TEAM)) + geom_bar()
How can I label the bars with the results from the y axis, when there's no y axis defined? (without altering the df)

1

votes

2

answer

1.9k

Views

### Removing latitude and longitude labels in ggplot

I'm creating a map of Minnesota
map.test

1

votes

1

answer

225

Views

### How to plot a vector of hex code colours

I would like to visualise a vector of hex code colours. It would be nice if the solution uses tiny square boxes so that they look like a ggplot2 legend:
These colour vectors are obtained either from the RColorBrewer package or are generated using the default ggplot2 hue:
get_gg_color_hue RColorBrew...

0

votes

0

answer

15

Views

### How to change xlim or scale of each facet in ggplot based on quantitle?

library(tidyr)
library(ggplot2)
df

1

votes

1

answer

8

Views

### ggplot stop geom_segment showing over geom_boxplot in legend

I am trying to get something similar to Glen_b's answer to this question. Basically, I want an arrow at the top of my boxplot to indicate that there are more outliers out of scale using ggplot.
I can get the main part of the plot to look as I'd like, but I am running into problems getting a sensible...

1

votes

2

answer

915

Views

### Violin Plot in R using ggplot2 on multiple data columns

I am new to R, and trying to make violin plots of species count data for various species at each sampling depth. The data looks like the following
Depth Cd Cf Cl
1 3.6576 0 2 0
2 4.0000 2 13 0
3 4.2672 0 0 0
4 13.1064 0 2 0
5 14.0000 3 17 10
6 17.0000 0 0 0
With species in columns...