geom_smooth does not plot a line for my data frame - r

I have a dataframe with the following data
my2016.regression.dataframe <- structure(list(Economy_Directorate = structure(c(9L, 1L, 18L,
11L, 5L, 7L), .Label = c("20128895", "25392278", "26802176",
"33214069", "34194316", "34863777", "34867843", "36497785", "37280694",
"37411816", "44460126", "45484123", "47463441", "48354697", "57954259",
"60187650", "65135916", "67317188"), class = "factor"), People_Directorate = structure(c(12L,
14L, 17L, 16L, 13L, 15L), .Label = c("20128895", "25392278",
"26802176", "33214069", "34194316", "34863777", "34867843", "36497785",
"37280694", "37411816", "44460126", "45484123", "47463441", "48354697",
"57954259", "60187650", "65135916", "67317188"), class = "factor")), .Names = c("Economy_Directorate",
"People_Directorate"), row.names = c(NA, -6L), class = "data.frame")
I used the following code to plot it. it plotts the points, but it does not plot the lm .
Could you help me why it does not plot the the lm in the geom_smooth
library(ggplot2)
ggplot(data =my2016.regression.dataframe )+
geom_point(aes(y=Economy_Directorate,x=People_Directorate))+
geom_smooth(method = "lm",aes(y=Economy_Directorate,x=People_Directorate),
fill="orange",colour="red")
Regards,

You need to convert your columns to numeric types. They are currently factors:
my2016.regression.dataframe$Economy_Directorate = as.numeric(as.character(my2016.regression.dataframe$Economy_Directorate))
my2016.regression.dataframe$People_Directorate = as.numeric(as.character(my2016.regression.dataframe$People_Directorate))
ggplot(data = my2016.regression.dataframe) +
geom_point(aes(y=Economy_Directorate,x=People_Directorate))+
geom_smooth(method = "lm",aes(y=Economy_Directorate,x=People_Directorate),
fill="orange",colour="red")

Related

How to solve: geom_linerange requires the following missing aesthetics: x, ymin, ymax

I am facing this error. I tried the solutions discussed here and here, to no avail. Clearly, there is something I am missing, not certain what. Any help would be much appreciated.
dt3<-structure(list(employee_name = structure(c(1L, 2L, 3L, 1L, 2L,
3L), .Label = c("A", "B", "C"), class = "factor"), min_salary = c(10L,
11L, 15L, 15L, 11L, 10L), mean_salary = c(15L, 16L, 16L, 16L,
16L, 15L), max_salary = c(20L, 21L, 17L, 17L, 21L, 20L), category_boss = structure(c(1L,
1L, 1L, 2L, 2L, 2L), .Label = c("Junior", "Senior"), class = "factor")), class = "data.frame", row.names = c(NA,
-6L))
ggplot(dt3) + geom_point(aes(x=mean_salary,y=employee_name,colour=category_boss),position = position_dodge(-.5)) +
geom_linerange(aes(xmin=min_salary,xmax=max_salary,y=employee_name,colour=category_boss),
position = position_dodge(-.5))
Warning: Ignoring unknown aesthetics: y, xmin, xmax
Error: geom_linerange requires the following missing aesthetics: x, ymin, ymax
The geom_linerange only allows a range for y as indicated by the error. So Just flip your x and y values, and then use coord_flip to swap the x and y axes when plotting.
ggplot(dt3) +
geom_point(aes(y=mean_salary, x=employee_name, colour=category_boss),
position = position_dodge(-.5)) +
geom_linerange(aes(ymin=min_salary, ymax=max_salary, x=employee_name, colour=category_boss),
position = position_dodge(-.5)) +
coord_flip()

How to make a lineplot with specific values out of a dataframe

I have a df as follow:
Variable Value
G1_temp_0 37.9
G1_temp_5 37.95333333
G1_temp_10 37.98333333
G1_temp_15 38.18666667
G1_temp_20 38.30526316
G1_temp_25 38.33529412
G1_mean_Q1 38.03666667
G1_mean_Q2 38.08666667
G1_mean_Q3 38.01
G1_mean_Q4 38.2
G2_temp_0 37.9
G2_temp_5 37.95333333
G2_temp_10 37.98333333
G2_temp_15 38.18666667
G2_temp_20 38.30526316
G2_temp_25 38.33529412
G2_mean_Q1 38.53666667
G2_mean_Q2 38.68666667
G2_mean_Q3 38.61
G2_mean_Q4 38.71
I like to make a lineplot with two lines which reflects the values "G1_mean_Q1 - G1_mean_Q4" and "G2_mean_Q1 - G2_mean_Q4"
In the end it should more or less look like this, the x axis should represent the different variables:
The main problem I have is, how to get a basic line plot with this df.
I've tried something like this,
ggplot(df, aes(x = c(1:4), y = Value) + geom_line()
but I have always some errors. It would be great if someone could help me. Thanks
Please post your data with dput(data) next time. it makes it easier to read your data into R.
You need to tell ggplot which are the groups. You can do this with aes(group = Sample). For this purpose, you need to restructure your dataframe a bit and separate the Variable into different columns.
library(tidyverse)
dat <- structure(list(Variable = structure(c(5L, 10L, 6L, 7L, 8L, 9L,
1L, 2L, 3L, 4L, 15L, 20L, 16L, 17L, 18L, 19L, 11L, 12L, 13L,
14L), .Label = c("G1_mean_Q1", "G1_mean_Q2", "G1_mean_Q3", "G1_mean_Q4",
"G1_temp_0", "G1_temp_10", "G1_temp_15", "G1_temp_20", "G1_temp_25",
"G1_temp_5", "G2_mean_Q1", "G2_mean_Q2", "G2_mean_Q3", "G2_mean_Q4",
"G2_temp_0", "G2_temp_10", "G2_temp_15", "G2_temp_20", "G2_temp_25",
"G2_temp_5"), class = "factor"), Value = c(37.9, 37.95333333,
37.98333333, 38.18666667, 38.30526316, 38.33529412, 38.03666667,
38.08666667, 38.01, 38.2, 37.9, 37.95333333, 37.98333333, 38.18666667,
38.30526316, 38.33529412, 38.53666667, 38.68666667, 38.61, 38.71
)), class = "data.frame", row.names = c(NA, -20L))
dat <- dat %>%
filter(str_detect(Variable, "mean")) %>%
separate(Variable, into = c("Sample", "mean", "time"), sep = "_")
g <- ggplot(data=dat, aes(x=time, y=Value, group=Sample)) +
geom_line(aes(colour=Sample))
g
Created on 2020-07-20 by the reprex package (v0.3.0)

How to change POSIXct objects display in facets title?

I am trying to create a ggplot2 graph using facet_grid(). Each facet has to be entitled with a date (here a POSIXct object) and I would like to change the way it is displayed.
How can i control the way POSIXct objects displays in ggplot2 facets title ?
Ex : this is how it is displayed : "2019-03-29"
and here is how I would like to see it written : "29/03/2018"
I have already looked at the labeller function but I can't figure out how to use it to change the way POSIXct object display. Maybe I am missing something.
I know facet labels can be "manually" changed but here I want a solution that works for any POSIXct object.
# create a dummy dataframe named ex
ex = structure(list(date = structure(c(1510531200, 1510531200, 1522195200,
1522195200), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
cat = c("a", "b", "a", "b"), measure = c(0.0777420913800597,
0.71574708330445, 0.725231731543317, 0.217509124660864)), row.names = c(NA,
-4L), vars = "date", indices = list(0:1, 2:3), group_sizes = c(2L,
2L), biggest_group_size = 2L, labels = structure(list(date = structure(c(1510531200,
1522195200), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA,
-2L), class = "data.frame", vars = "date", indices = list(c(0L,
1L, 8L, 9L, 16L, 17L), c(2L, 3L, 4L, 5L, 10L, 11L, 12L, 13L,
18L, 19L, 20L, 21L), c(6L, 7L, 14L, 15L, 22L, 23L)), group_sizes = c(6L,
12L, 6L), biggest_group_size = 12L, labels = structure(list(date = structure(c(1510531200,
1522195200, 1543881600), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA,
-3L), class = "data.frame", vars = "date"), drop = TRUE), drop = TRUE, class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
# create a graph
plot_ex = ggplot(ex, aes(x = cat, y = measure)) +
geom_bar(stat = "identity") +
facet_grid(.~date)
print(plot_ex)
The facets are named "2017-11-13" and "2018-03-28". I want them to be "13/11/2017" and "28/03/2018".
Many thanks for your help,
You can change how dates are printed with format. Using that, we can set an appropriate labeller, without changing the data.frame column.
ggplot(ex, aes(x = cat, y = measure)) +
geom_bar(stat = "identity") +
facet_grid(.~date, labeller = function(x) format(x, '%d/%m/%Y'))
We can use strftime.
ex$date <- strftime(ex$date, format="%d/%m/%Y")
library(ggplot2)
plot_ex <- ggplot(ex, aes(x=cat, y=measure)) +
geom_bar(stat="identity") +
facet_grid(.~date)
print(plot_ex)

wrong linking point with lines in ggplot

I don't know what I'm missing but I cannot figure out a very simple task. This is a small piece of my dataframe:
dput(df)
structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = "SOU55", class = "factor"), Depth = c(2L, 4L,
6L, 8L, 10L, 12L, 14L, 16L, 18L, 20L), Value = c(211.8329815,
278.9603866, 255.6111086, 212.6163368, 193.7281895, 200.9584658,
160.9289157, 192.0664419, 174.5951019, 7.162682425)), .Names = c("ID",
"Depth", "Value"), class = "data.frame", row.names = c(NA, -10L
))
What I'm trying to do is simply plotting Depth versus Value with ggplot, this is the simple code:
ggplot(df, aes(Value, Depth))+
geom_point()+
geom_line()
and this the result:
But it is pretty different from what I really want. This is the plot made with Libreoffice:
It seems that ggplot doesn't link correctly the values. What am I doing wrong?
Thanks to all!
You need geom_path() to connect the observations in the original order. geom_line() sorts the data according to the x-aesthetic before plotting:
ggplot(df, aes(Value, Depth))+
geom_point()+
geom_path()

set minimum limit for violin plot ggplot

I'd like to set the minimum bounds for a violin plot, similar to this question: set only lower bound of a limit for ggplot
For this:
p <- ggplot(somedf, aes(factor(user1), pq)) + aes(ymin = -50)
p + geom_violin(aes(fill = user1))+ aes(ymin=-50)
I've tried adding
+ expand_limits(y=-50)
and
+ aes(ymin = -50)
to set lower bounds with no effect.
Here's a sample dataframe that results in the same problem:
structure(list(pq = c(-20L, -12L, 10L, -13L, 11L, -16L), time = c(1214.1333,
1214.1833, 1214.2667, 1214.2833, 1214.35, 1214.5167), pq.1 = c(-20L,
-12L, 10L, -13L, 11L, -16L), time.1 = c(1214.1333, 1214.1833,
1214.2667, 1214.2833, 1214.35, 1214.5167), time.2 = c(1214.1333,
1214.1833, 1214.2667, 1214.2833, 1214.35, 1214.5167), pq.2 = c(-20L,
-12L, 10L, -13L, 11L, -16L), user1 = structure(c(1L, 1L, 2L,
1L, 2L, 1L), .Label = c("someguy3", "someguy4", "someguy6", "someguy4",
"someguy5", "someguy6"), class = "factor"), pq.3 = c(-20L, -12L, 10L,
-13L, 11L, -16L), time.3 = c(1214.1333, 1214.1833, 1214.2667,
1214.2833, 1214.35, 1214.5167), user1.1 = structure(c(1L, 1L,
2L, 1L, 2L, 1L), .Label = c("someguy3", "someguy4", "someguy6",
"someguy4", "someguy5", "someguy6"), class = "factor")), .Names = c("pq",
"time", "pq.1", "time.1", "time.2", "pq.2", "user1", "pq.3",
"time.3", "user1.1"), row.names = c(565L, 566L, 568L, 569L, 570L,
574L), class = "data.frame")
ggplot will pay attention to the aes() directive if you add a call to geom_blank().
## A reproducible example
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
## This doesn't work:
p + aes(ymin = -10) + geom_violin()
## But this does:
p + aes(ymin = -10) + geom_violin() + geom_blank()
(Note: For this example at least, expand_limits(y = -10) works with or without an accompanying call to geom_blank().)

Resources