Related
I have to do a ggplot barplot with errorbars, Tukey sig. letters for plants grown with different fertilizer concentraitions.
The data should be grouped after the dif. concentrations and the sig. letters should be added automaticaly.
I have already a code for the same problem but for Boxplot - which is working nicely. I tried several tutorials with barplots but I always get the problem; stat_count() can only have an x or y aesthetic.
So I thought, is it possible to get my boxplot code to a barplot code? I tried but I couldnt do it :) And if not - how do I automatically add tukeyHSD Test result sig. letters to a ggplot barplot?
This is my Code for the boxplot with the tukey letters:
value_max = Dünger, group_by(Duenger.g), summarize(max_value = max(Höhe.cm))
hsd=HSD.test(aov(Höhe.cm~Duenger.g, data=Dünger),
trt = "Duenger.g", group = T) sig.letters <- hsd$groups[order(row.names(hsd$groups)), ]
J <- ggplot(Dünger, aes(x = Duenger.g, y = Höhe.cm))+ geom_boxplot(aes(fill= Duenger.g))+ scale_fill_discrete(labels=c("0.5g", '1g', "2g", "3g", "4g"))+ geom_text(data = value_max, aes(x=Duenger.g, y = 0.1 + max_value, label = sig.letters$groups), vjust=0)+ stat_boxplot(geom = 'errorbar', width = 0.1)+ ggtitle("Auswirkung von Dünger auf die Höhe von Pflanzen") + xlab("Dünger in g") + ylab("Höhe in cm"); J
This is how it looks:
boxplot with tukey
Data from dput:
structure(list(Duenger.g = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4), plant = c(1, 2, 3, 4, 5, 7, 10, 11, 12, 13, 14, 18, 19,
21, 23, 24, 25, 26, 27, 29, 30, 31, 33, 34, 35, 37, 38, 39, 40,
41, 42, 43, 44, 48, 49, 50, 53, 54, 55, 56, 57, 58, 61, 62, 64,
65, 66, 67, 68, 69, 70, 71, 72, 73, 75, 79, 80, 81, 83, 85, 86,
88, 89, 91, 93, 99, 100, 102, 103, 104, 105, 106, 107, 108, 110,
111, 112, 113, 114, 115, 116, 117, 118, 120, 122, 123, 125, 126,
127, 128, 130, 131, 132, 134, 136, 138, 139, 140, 141, 143, 144,
145, 146, 147, 149), height.cm = c(5.7, 2.8, 5.5, 8, 3.5, 2.5,
4, 6, 10, 4.5, 7, 8.3, 11, 7, 8, 2.5, 7.4, 3, 14.5, 7, 12, 7.5,
30.5, 27, 6.5, 19, 10.4, 12.7, 27.3, 11, 11, 10.5, 10.5, 13,
53, 12.5, 12, 6, 12, 35, 8, 16, 56, 63, 69, 62, 98, 65, 77, 32,
85, 75, 33.7, 75, 55, 38.8, 39, 46, 35, 59, 44, 31.5, 49, 34,
52, 37, 43, 38, 28, 14, 28, 19, 20, 23, 17.5, 32, 16, 17, 24.7,
34, 50, 12, 14, 21, 33, 39.3, 41, 29, 35, 48, 40, 65, 35, 10,
26, 34, 41, 32, 38, 23.5, 22.2, 20.5, 29, 34, 45)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -105L))
Thank you
mirai
A bar chart and a boxplot are two different things. By default geom_boxplot computes the boxplot stats by default (stat="boxplot"). In contrast when you use geom_bar it will by default count the number of observations (stat="count") which are then mapped on y. That's the reason why you get an error. Hence, simply replacing geom_boxplot by geom_bar will not give your your desired result. Instead you could use e.g. stat_summary to create your bar chart with errorbars. Additionally I created a summary dataset to add the labels on the top of the error bars.
library(ggplot2)
library(dplyr)
library(agricolae)
Dünger <- Dünger |>
rename("Höhe.cm" = height.cm) |>
mutate(Duenger.g = factor(Duenger.g))
hsd <- HSD.test(aov(Höhe.cm ~ Duenger.g, data = Dünger), trt = "Duenger.g", group = T)
sig.letters <- hsd$groups %>% mutate(Duenger.g = row.names(.))
duenger_sum <- Dünger |>
group_by(Duenger.g) |>
summarize(mean_se(Höhe.cm)) |>
left_join(sig.letters, by = "Duenger.g")
ggplot(Dünger, aes(x = Duenger.g, y = Höhe.cm, fill = Duenger.g)) +
stat_summary(geom = "bar", fun = "mean") +
stat_summary(geom = "errorbar", width = .1) +
scale_fill_discrete(labels = c("0.5g", "1g", "2g", "3g", "4g")) +
geom_text(data = duenger_sum, aes(y = ymax, label = groups), vjust = 0, nudge_y = 1) +
labs(
title = "Auswirkung von Dünger auf die Höhe von Pflanzen",
x = "Dünger in g", y = "Höhe in cm"
)
#> No summary function supplied, defaulting to `mean_se()`
But as the summary dataset now already contains the mean and the values for the error bars a second option would be to do:
ggplot(duenger_sum, aes(x = Duenger.g, y = y, fill = Duenger.g)) +
geom_col() +
geom_errorbar(aes(ymin = ymin, ymax = ymax), width = .1) +
scale_fill_discrete(labels = c("0.5g", "1g", "2g", "3g", "4g")) +
geom_text(aes(y = ymax, label = groups), vjust = 0, nudge_y = 1) +
labs(
title = "Auswirkung von Dünger auf die Höhe von Pflanzen",
x = "Dünger in g", y = "Höhe in cm"
)
I want to add a series of arrows connecting each observation in geom_point as in the graph:
I understand that geom_segment is meant to be used, but I am having issues, and have not found something quite like this after quite a bit of searching.
This is sample code that should satisfy the pattern:
Note: The labels are not important ; just the arrows
df <- data.frame(year = c(1935:1968),
y_axis_values = c( 2755,2696, 2646, 2701, 2654, 2766, 2832, 2964, 3041, 3010, 3018, 3374, 3545, 3441, 3456, 3455, 3503, 3641, 3721, 3828, 3831, 3858, 3925, 3880, 3935, 3895, 3840, 3756, 3669, 3502, 3145, 2812, 2586,2441),
x_axis_values = c(238, 240, 241, 242, 244, 245, 246, 268, 333, 335, 331, 253, 243, 241, 242, 237, 242, 240, 233, 232, 236, 245, 256, 261, 265, 278, 291, 290, 290, 307, 313, 325, 339, 338)
I have tried the general formula with many different argument variations, but cannot seem to find it.
ggplot(df, aes(x = x_axis_values, y = y_axis_values) +
geom_point() +
geom_segment()
You need the xend and yend values for each segment. Since your data frame is in order, the xend and yend value for each segment is just the next row's x and y values. You can get these by using dplyr::lead on the x and y aesthetics.
library(ggplot2)
library(dplyr)
ggplot(df, aes(x = x_axis_values, y = y_axis_values)) +
geom_point(color = "#69b3a2") +
geom_segment(aes(xend = after_stat(lead(x)), yend = after_stat(lead(y))),
arrow = arrow(length = unit(3, "mm")), color = "#69b3a2") +
geom_text(aes(label = year), size = 5, fontface = 2,
data = . %>% filter(year %in% c(1935, 1937, 1939, 1942, 1945, 1946,
1953, 1957, 1960, 1961)),
nudge_x = c(-3, -2, 4, 0, 0, -2, -5, 0, 3, 5),
nudge_y = c(30, -30, 10, -30, -40, -40, 0, -50, 30, 0)) +
labs(x = "partic", y = "tfr") +
theme_bw(base_size = 16)
I have a dataset with the F1 teams and points they scored since the beginning with 203 rows.
I would like to make a barplot, but color-filling only the bars for constructors from 2020, and leave the rest in one colour. Is it even possible?
Find data below. The code I'm using to plot so far is:
ggplot(data=top_constructors[1:input$top1,], aes(x=reorder(name, total_points), y=total_points, fill=name)) +
geom_bar(stat = "identity") +
coord_flip() +
xlab("Constructor") +
ylab("Total points") +
scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9", rep("black", 200)))
The code above is coloring 3 first values alphabetically, and I would like to set certain colors for certain teams, eg. red for Ferrari etc.
Thank you for your help.
dput(top_constructors) gives such an output:
structure(list(name = c("Ferrari", "Mercedes", "McLaren", "Red Bull",
"Williams", "Renault", "Force India", "Team Lotus", "Benetton",
"Tyrrell", "Lotus F1", "Brabham", "Sauber", "BRM", "Toro Rosso",
"Ligier", "Cooper-Climax", "Maserati", "BMW Sauber", "Jordan",
"Racing Point", "Lotus-Climax", "Alfa Romeo", "Toyota", "BAR",
"Lotus-Ford", "Haas F1 Team", "Brabham-Repco", "Brawn", "Honda",
"March", "McLaren-Ford", "Arrows", "Kurtis Kraft", "Matra-Ford",
"Vanwall", "AlphaTauri", "Cooper-Maserati", "Wolf", "Brabham-Climax",
"Brabham-Ford", "Shadow", "Surtees", "Matra", "Cooper", "Porsche",
"Jaguar", "Hesketh", "Stewart", "Fittipaldi", "Epperly", "Minardi",
"March-Ford", "Watson", "Prost", "Lotus-BRM", "Lola", "Toleman",
"Footwork", "Gordini", "Talbot-Lago", "Penske", "Larrousse",
"Kuzma", "Cooper-BRM", "Ensign", "Brabham-Alfa Romeo", "Connaught",
"Dallara", "Brabham-BRM", "Eagle-Weslake", "BRP", "Lesovsky",
"Deidt", "Shadow-Ford", "Lancia", "Leyton House", "ATS", "Phillips",
"Onyx", "Rial", "Parnelli", "Iso Marlboro", "McLaren-BRM", "Osella",
"Simca", "Super Aguri", "Eagle-Climax", "Embassy Hill", "Frazer Nash",
"Sherman", "Cooper-Castellotti", "AGS", "Zakspeed", "Theodore",
"HWM", "Schroeder", "Marussia", "Spyker", "Tecno", "Trevis",
"McLaren-Serenissima", "Manor Marussia", "MF1", "Spyker MF1",
"Forti", "Pacific", "Simtek", "Fondmetal", "Andrea Moda", "Lambo",
"Coloni", "Euro Brun", "Life", "RAM", "Spirit", "Merzario", "Kauhsen",
"Rebaque", "Martini", "LEC", "McGuire", "Boro", "Apollon", "Kojima",
"Maki", "Lyncar", "Trojan", "Amon", "Token", "Politoys", "Connew",
"Bellasi", "De Tomaso", "LDS", "Protos", "Shannon", "Scirocco",
"RE", "Derrington", "Gilby", "Stebro", "Emeryson", "ENB", "JBW",
"Ferguson", "MBM", "Behra-Porsche", "Scarab", "Meskowski", "Christensen",
"Ewing", "Aston Martin", "Moore", "Dunn", "Elder", "Sutton",
"Fry", "Tec-Mec", "Alta", "OSCA", "Stevens", "Bugatti", "Pawl",
"Pankratz", "Arzani-Volpini", "Nichels", "Bromme", "Klenk", "Turner",
"Del Roy", "Veritas", "BMW", "EMW", "AFM", "ERA", "Aston Butterworth",
"Cisitalia", "Hall", "Marchese", "Langley", "Rae", "Olson", "Wetteroth",
"Adams", "Snowberger", "Milano", "HRT", "Virgin", "Cooper-OSCA",
"Cooper-Borgward", "Lotus-Maserati", "De Tomaso-Osca", "De Tomaso-Alfa Romeo",
"Lotus-Borgward", "Cooper-Alfa Romeo", "De Tomaso-Ferrari", "LDS-Climax",
"LDS-Alfa Romeo", "Cooper-Ford", "Cooper-Ferrari", "Cooper-ATS",
"BRM-Ford", "McLaren-Alfa Romeo", "March-Alfa Romeo", "Lotus-Pratt & Whitney",
"Shadow-Matra", "Lotus", "Caterham"), total_points = c(9292.77,
5824.14, 5723.5, 5043.5, 3567, 1777, 1098, 995, 861.5, 711, 706,
631, 557, 537.5, 500, 388, 336.5, 313.14, 308, 291, 283, 281,
279, 278.5, 227, 209, 200, 175, 172, 156, 148, 143, 142, 130,
130, 108, 107, 83, 79, 78, 68, 59, 54, 54, 52, 50, 49, 48, 47,
44, 44, 38, 37, 36, 35, 29, 27, 26, 25, 25, 25, 23, 22, 21, 20,
19, 18, 17, 15, 13, 13, 11, 10, 10, 9.5, 9, 8, 7, 7, 6, 6, 6,
6, 6, 5, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0)), row.names = c(NA, -209L), class = c("tbl_df", "tbl", "data.frame"
))
You could define named colors (or just a subset with specified colors) and use scale_fill_identity:
library(ggplot2)
library(dplyr)
topN <- 30 # limit graph to the 30 teams with most points
t2020 <- c("Mercedes", "Red Bull", "McLaren", "Racing Point", "Renault", "Ferrari",
"AlphaTauri", "Alfa Romeo", "Haas F1 Team", "Williams") # 2020 teams
top_constructors <- mutate(top_constructors, active_in_2020 = ifelse(name %in% t2020, T, F))
rest <- filter(top_constructors, !active_in_2020)$name # non-2020 teams
teamcolors <- c(setNames(scales::hue_pal()(length(t2020)), t2020),
setNames(rep("black", length(rest)), rest)) # only color 2020 teams
teamcolors[c("Ferrari", "Mercedes")] <- c("red", "grey70") # change colors for some teams
arrange(top_constructors, desc(total_points)) %>%
slice_head(n=topN) %>%
mutate(name=factor(name, name),
clr=teamcolors[as.character(name)]) %>%
ggplot(aes(x=name, y=total_points, fill=clr)) +
geom_col()+
scale_x_discrete(limits=rev) +
coord_flip() +
labs(x="Constructor", y="Total points") +
scale_fill_identity()
Created on 2020-12-22 by the reprex package (v0.3.0)
The gghighlight package is built specifically for this use and can be very helpful. Check out the vignette here for more info.
In your case I think are trying to specify the top number of teams to include in the plot but it wasn't included in your question so I've added that below:
library(gghighlight)
top1 <- 10 # looks like you're calling input$top1 for this value but didn't share it
top_constructors %>%
slice_max(order_by = total_points, n = top1) %>%
ggplot(aes(
x = reorder(name, total_points),
y = total_points,
fill = name
)) +
geom_col() +
gghighlight::gghighlight(
total_points,
max_highlight = 3,
unhighlighted_params = list(fill = alpha("black", 0.4))
) +
scale_fill_manual(values = c("#FF0000", "#E69F00", "#56B4E9")) +
coord_flip() +
xlab("Constructor") +
ylab("Total points")
This produces the following plot:
A few notes:
You requested coloring all the non-highlighted bars "black" so I put in the unhighlighted_params argument so you can see how to control that but the alpha was just for example.
You mentioned that Ferrari should be "red", so I modified the manual colors you provided.
When using gghighlight, you only need to provide enough colors for the highlighted bars to scale_fill_manual. However if you provide fewer than the number of highlighted bars, it will throw an error.
I am fairly new to R and am attempting to plot data frames simultaneously using ggplot2.
I have two data frames.
One is called WorkSchedMonday and consist of 96 rows and 4 columns.
structure(c(9, 9, 9, 9, 18, 18, 36, 36, 36, 36, 64, 80, 96, 96,
112, 128, 168, 168, 296, 312, 14, 14, 14, 21, 21, 21, 21, 35,
49, 49, 12, 12, 6, 6, 0, 0, 0, 0, 6, 6), .Dim = c(10L, 4L), .Dimnames = list(
c("04:00", "04:15", "04:30", "04:45", "05:00", "05:15", "05:30",
"05:45", "06:00", "06:15"), c("WorkSchedAndIndivMondayAtHome",
"WorkSchedAndIndivMondayAtSingleWorkPlace", "WorkSchedAndIndivMondayAtVarietyOfPlaces",
"WorkSchedAndIndivMondayWorkingOnTheMove")))
The other is called WorkSchedTuesday and consist of 96 rows and 4 columns.
structure(c(0, 0, 0, 0, 9, 9, 27, 27, 36, 36, 64, 80, 96, 96,
112, 128, 168, 168, 296, 312, 14, 14, 14, 21, 21, 21, 21, 35,
49, 49, 12, 12, 6, 6, 0, 0, 0, 0, 6, 6), .Dim = c(10L, 4L), .Dimnames = list(
c("04:00", "04:15", "04:30", "04:45", "05:00", "05:15", "05:30",
"05:45", "06:00", "06:15"), c("WorkSchedAndIndivTuesdayAtHome",
"WorkSchedAndIndivTuesdayAtSingleWorkPlace", "WorkSchedAndIndivTuesdayAtVarietyOfPlaces",
"WorkSchedAndIndivTuesdayWorkingOnTheMove")))
Using the following code a plotted the 2 data frames.
WorkSchedWeek<-as.matrix(cbind(WorkSchedAndIndivMondayAtHome,WorkSchedAndIndivMondayAtSingleWorkPlace,WorkSchedAndIndivMondayAtVarietyOfPlaces, WorkSchedAndIndivMondayWorkingOnTheMove, WorkSchedAndIndivTuesdayAtHome,WorkSchedAndIndivTuesdayAtSingleWorkPlace,WorkSchedAndIndivTuesdayAtVarietyOfPlaces, WorkSchedAndIndivTuesdayWorkingOnTheMove))
####
melted_WorkSchedWeek<- melt(WorkSchedWeek)
plot<-ggplot(melted_WorkSchedWeek) + geom_col(aes(x = Var1,y = value,fill = Var2),position = "fill") + theme(legend.position="right", axis.text.x = element_text(angle = 90, hjust = 1))
plot + labs(x="Time", y="Probabilities", colour="Work schedules", fill="Work schedules")
However I would like to create the above plot using ggplot (or lattice) . On x axis is time (0400 till 0345 _ 24hours) per days (Monday and Tuesday), y axis probability distributions. The plot is filled with work schedules values. Can somebody help me? Thanks
You can use facet_grid to make two graphs side by side but sharing an axis. But this requires you to first merge your two dataframes.
To do this we standardize your variables, add a day column, a time column and then use rbind:
WorkSchedMonday = data.frame(structure(c(9, 9, 9, 9, 18, 18, 36, 36, 36, 36, 64, 80, 96, 96,
112, 128, 168, 168, 296, 312, 14, 14, 14, 21, 21, 21, 21, 35,
49, 49, 12, 12, 6, 6, 0, 0, 0, 0, 6, 6), .Dim = c(10L, 4L), .Dimnames = list(
c("04:00", "04:15", "04:30", "04:45", "05:00", "05:15", "05:30",
"05:45", "06:00", "06:15"), c("WorkSchedAndIndivMondayAtHome",
"WorkSchedAndIndivMondayAtSingleWorkPlace", "WorkSchedAndIndivMondayAtVarietyOfPlaces",
"WorkSchedAndIndivMondayWorkingOnTheMove"))))
names(WorkSchedMonday) = c("AtHome", "SingleWork", "Variety", "OnTheMove")
WorkSchedMonday$time = rownames(WorkSchedMonday)
WorkSchedTuesday = data.frame(structure(c(0, 0, 0, 0, 9, 9, 27, 27, 36, 36, 64, 80, 96, 96,
112, 128, 168, 168, 296, 312, 14, 14, 14, 21, 21, 21, 21, 35,
49, 49, 12, 12, 6, 6, 0, 0, 0, 0, 6, 6), .Dim = c(10L, 4L), .Dimnames = list(
c("04:00", "04:15", "04:30", "04:45", "05:00", "05:15", "05:30",
"05:45", "06:00", "06:15"), c("WorkSchedAndIndivMondayAtHome",
"WorkSchedAndIndivMondayAtSingleWorkPlace", "WorkSchedAndIndivMondayAtVarietyOfPlaces",
"WorkSchedAndIndivMondayWorkingOnTheMove"))))
names(WorkSchedTuesday) = c("AtHome", "SingleWork", "Variety", "OnTheMove")
WorkSchedTuesday$time = rownames(WorkSchedTuesday)
WorkSchedMonday$day = "Monday"
WorkSchedTuesday$day = "Tuesday"
WorkSched = rbind(WorkSchedMonday, WorkSchedTuesday)
With that done, you can melt your dataframe like you did before and run the same ggplot, but with facet_grid along the variable that you want your graph to be separated by (day).
WorkSched_melt = melt(WorkSched, id.vars = c("time", "day"))
ggplot(WorkSched_melt, aes(x = time, y = value, fill = variable)) + geom_col(position = "fill") +
facet_grid(. ~ day) + theme(legend.position="right", axis.text.x = element_text(angle = 90, hjust = 1))
As a general rule, avoid using really big and clunky variable names, and also avoid having a necessary variable (in this case, time) as your row name.
Here is a solution with the data preparation code done with package dplyr.
library(ggplot2)
library(dplyr)
WorkSchedWeek <- cbind(WorkSchedMonday, WorkSchedTuesday)
WorkSchedWeek <- as.data.frame(WorkSchedWeek)
WorkSchedWeek <- cbind.data.frame(Hour = row.names(WorkSchedWeek), WorkSchedWeek)
melted_WorkSchedWeek <- reshape2::melt(WorkSchedWeek, id.vars = "Hour")
melted_WorkSchedWeek %>%
mutate(variable = sub("^WorkSchedAndIndiv", "", variable),
Month = sub("(^.{3}).*", "\\1", variable),
variable = sub("^.*day", "", variable)) %>%
ggplot(aes(x = Hour,y = value, fill = variable)) +
geom_col(position = "fill") +
theme(legend.position = "right",
axis.text.x = element_text(angle = 90, hjust = 1)) +
facet_wrap(~ Month)
Im creating a ggplot with geom_vline at a specific location on the x axis. i would like the x axis to show that specific value
Following is my data + code:
dput(agg_data)
structure(list(latency = structure(c(0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 24, 26, 28,
29, 32, 36, 37, 40, 43, 46, 47, 48, 49, 54, 64, 71, 72, 75, 87,
88, 89, 93, 134, 151), class = "difftime", units = "days"), count = c(362,
11, 8, 5, 4, 2, 8, 6, 4, 2, 2, 1, 5, 1, 2, 2, 2, 1, 1, 1, 2,
1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1,
1, 1, 1), cum_sum = c(362, 373, 381, 386, 390, 392, 400, 406,
410, 412, 414, 415, 420, 421, 423, 425, 427, 428, 429, 430, 432,
433, 435, 436, 437, 438, 439, 440, 441, 442, 444, 446, 447, 448,
449, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460), cum_dist = c(0.78695652173913,
0.810869565217391, 0.828260869565217, 0.839130434782609, 0.847826086956522,
0.852173913043478, 0.869565217391304, 0.882608695652174, 0.891304347826087,
0.895652173913044, 0.9, 0.902173913043478, 0.91304347826087,
0.915217391304348, 0.919565217391304, 0.923913043478261, 0.928260869565217,
0.930434782608696, 0.932608695652174, 0.934782608695652, 0.939130434782609,
0.941304347826087, 0.945652173913043, 0.947826086956522, 0.95,
0.952173913043478, 0.954347826086957, 0.956521739130435, 0.958695652173913,
0.960869565217391, 0.965217391304348, 0.969565217391304, 0.971739130434783,
0.973913043478261, 0.976086956521739, 0.980434782608696, 0.982608695652174,
0.984782608695652, 0.98695652173913, 0.989130434782609, 0.991304347826087,
0.993478260869565, 0.995652173913044, 0.997826086956522, 1)), .Names = c("latency",
"count", "cum_sum", "cum_dist"), row.names = c(NA, -45L), class = "data.frame")
and code:
library(ggplot2)
library(ggthemes)
russ<-ggplot(data=agg_data,aes(x=as.numeric(latency),y=cum_dist))+geom_line(size=2)
russ<-russ+ggtitle("Latency from first click to Qualified Demo:") + xlab("in Days")+ ylab("Proportion of maturity")+theme_economist()
russ<-russ+geom_vline(aes(xintercept=10), color="black", linetype="dashed")
russ
which creates the following plot:
I want the plot show the value '10' (same location as the vline) on the x-axis
I looked for some other similar answers, like in Customize axis labels
but this one re creates the x axis labels (with scale_x_discrete), and does not add a new number to the current scale, which is more of what im looking for.
thanks in advance!
In your case x scale is continuous, so you can use function scale_x_continuous() and provide breaks at positions you need.
russ + scale_x_continuous(breaks=c(0,10,50,100,150))