Related
I'm trying to plot a graph for a likert test using ggplot2 and I would like to have the percentages values appearing on the graph. I've created a df with all the averages and percentages so I could write it on the graph. It all seems to be working good, except the values are being plotted as if they were upsided or something.
This is the code I'm using
example <- structure(list(grupo = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("EJA",
"REG"), class = "factor"), nivel = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("CINCO", "DOZE", "NOVE"), class = "factor"), tipo = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 3L), .Label = c("COR", "PAD", "RES"), class = "factor"),
likert = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L), .Label = c("0",
"1", "2", "3"), class = c("ordered", "factor")), cnt = c(3L,
1L, 3L, 5L, 3L, 1L, 3L, 6L, 2L, 1L, 10L, 5L, 5L, 9L, 11L,
6L, 4L, 10L, 10L, 10L), freq = c(0.25, 0.083, 0.25, 0.417,
0.231, 0.077, 0.231, 0.462, 0.154, 0.077, 0.769, 0.167, 0.167,
0.3, 0.367, 0.2, 0.133, 0.333, 0.333, 0.333), prop = c(25,
8.3, 25, 41.7, 23.1, 7.7, 23.1, 46.2, 15.4, 7.7, 76.9, 16.7,
16.7, 30, 36.7, 20, 13.3, 33.3, 33.3, 33.3), proptext = c("25",
"8.3", "25", "41.7", "23.1", "7.7", "23.1", "46.2", "15.4",
"7.7", "76.9", "16.7", "16.7", "30", "36.7", "20", "13.3",
"33.3", "33.3", "33.3")), row.names = c(NA, -20L), groups = structure(list(
grupo = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("EJA",
"REG"), class = "factor"), nivel = structure(c(1L, 1L, 1L,
2L, 2L, 2L), .Label = c("CINCO", "DOZE", "NOVE"), class = "factor"),
tipo = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("COR",
"PAD", "RES"), class = "factor"), .rows = structure(list(
1:4, 5:8, 9:11, 12:15, 16:19, 20L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
ggplot(example, aes(x=(interaction(grupo, nivel, tipo)),y=prop, fill=likert))+
geom_col()+
#scale_y_continuous(labels = percent)+
coord_flip() +
ggtitle("Testing")+
xlab("A, B, and C")+
ylab("%")+
geom_text(aes(label = proptext), size = 2, colour = "black")
Would someone have an idea of how could I solve it?
The geom_text may also require the x, y
library(dplyr)
library(tidyr)
library(ggplot2)
example %>%
unite(new, grupo, nivel, tipo, sep = ".") %>%
ggplot(aes(x=new, fill=likert))+
geom_col(aes(y= prop))+
geom_text(aes(x = new, y = prop, label = proptext),
position = position_stack(vjust = .5)) +
coord_flip() +
#scale_y_continuous(labels = percent)+
ggtitle("Testing")+
xlab("A, B, and C")+
ylab("%")
-output
I want to arrange the plot below so that 'group' is arranged in descending order by 'Distance' within Community (Out, In).
I've tried using dplyr::arrange and tidytext::reorder_within(group, -value, MPA_type), but neither of these work - ggplot continues to default to sorted by group name in descending order (see legend). If facetwrap is part of the solution, I'd prefer to have the labels at the bottom as in the example figure, rather than the typical facet header.
Here is my data:
sig_distance <- structure(list(Var1 = structure(c(2L, 2L, 5L, 5L, 2L, 2L, 5L,
5L, 2L, 2L, 5L, 5L, 2L, 2L, 5L, 5L, 2L, 2L, 5L, 5L), .Label = c("ref after",
"ref before", "ref during", "smr after", "smr before", "smr during"
), class = "factor"), Var2 = structure(c(1L, 3L, 4L, 6L, 1L,
3L, 4L, 6L, 1L, 3L, 4L, 6L, 1L, 3L, 4L, 6L, 1L, 3L, 4L, 6L), .Label = c("ref after",
"ref before", "ref during", "smr after", "smr before", "smr during"
), class = "factor"), value = c(0.0781171338765429, 0.070131485880327,
0.124219180798504, 0.0642584499973571, 0.16882716299913, 0.123057288279708,
0.185404402405965, 0.113660097900038, 0.14628853013894, 0.106462687516074,
0.179579889492142, 0.146317072898829, 0.163284273893779, 0.130083096905712,
0.0991349070859965, 0.106610448830353, 0.0499622399107518, 0.0563330614755333,
0.0391975833642552, 0.0435817314833789), MPA = structure(c(3L,
2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L,
2L, 4L, 1L), .Label = c("MPA___before-to-during", "Reference___before-to-during",
"Reference___before-to-after", "MPA___before-to-after"), scores = structure(c(`MPA___before-to-after` = 0.125507192629372,
`MPA___before-to-during` = 0.0948855602219911, `Reference___before-to-after` = 0.121295868163829,
`Reference___before-to-during` = 0.0972135240114708), .Dim = 4L, .Dimnames = list(
c("MPA___before-to-after", "MPA___before-to-during", "Reference___before-to-after",
"Reference___before-to-during"))), class = "factor"), name = c("sd_ref_pooled_before_after",
"sd_ref_pooled_before_during", "sd_smr_pooled_before_after",
"sd_smr_pooled_before_during", "sd_ref_pooled_before_after",
"sd_ref_pooled_before_during", "sd_smr_pooled_before_after",
"sd_smr_pooled_before_during", "sd_ref_pooled_before_after",
"sd_ref_pooled_before_during", "sd_smr_pooled_before_after",
"sd_smr_pooled_before_during", "sd_ref_pooled_before_after",
"sd_ref_pooled_before_during", "sd_smr_pooled_before_after",
"sd_smr_pooled_before_during", "sd_ref_pooled_before_after",
"sd_ref_pooled_before_during", "sd_smr_pooled_before_after",
"sd_smr_pooled_before_during"), sd_pooled = c(0.0640133632403095,
0.059224209496302, 0.0418411590759088, 0.0420366263878186, 0.0697371748889264,
0.0713939572229526, 0.0662209469675998, 0.0673861920919254, 0.0952259175162696,
0.0973881903133112, 0.104688903793631, 0.104123442035945, 0.0831395386888112,
0.0736773344066338, 0.0870890086043125, 0.082205340195828, 0.0622386704700814,
0.0506360166386964, 0.0776340589400514, 0.057490214920967), group = structure(c(1L,
1L, 1L, 1L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 2L,
2L, 2L, 2L), .Label = c("Group 5", "Group 4", "Group 3", "Group 2",
"Group 1"), class = "factor"), period = structure(c(1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L), .Label = c("Before-to-after", "Before-to-during"), class = "factor"),
MPA_type = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("Out",
"In"), class = "factor"), Df = c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), SumOfSqs = c(0.644667474362213,
0.163718859184403, 0.531883138574452, 0.172377817596512,
1.36062525175964, 0.561260198791761, 1.26106114133322, 0.478287656340437,
1.12522630961807, 0.502450621711331, 0.779126478533436, 0.349888339609262,
0.64822862542131, 0.198815304465667, 0.329502905266225, 0.124876688005652,
0.197752601704789, 0.10862759418973, 0.142471900603864, 0.0840130152284379
), R2 = c(0.190110542714351, 0.0631029576320754, 0.217228522650464,
0.089364183731598, 0.076782933708657, 0.0349456770960147,
0.0732327885619086, 0.0303186954655169, 0.0788241854776924,
0.0376889492950336, 0.0656167575864088, 0.0312644101580068,
0.288412156204619, 0.123882033933067, 0.158350315762715,
0.0838360162000489, 0.0226863909348303, 0.0137219062151816,
0.0222248102598127, 0.0150399647150649), F = c(9.62419276009816,
2.4247130389196, 11.3779943270621, 3.53281801227694, 10.4792794679964,
4.27290961706825, 9.00823614929462, 3.37679925864692, 10.610568411745,
4.50397942095095, 7.93538804068383, 3.45325589560756, 6.07961811151075,
1.83818447230295, 2.44585620771756, 1.18959949296437, 3.29624747150235,
1.85040460507006, 2.25026799494279, 1.42007459023087), `p-val` = c(0.001,
0.05, 0.001, 0.006, 0.001, 0.001, 0.001, 0.002, 0.001, 0.001,
0.001, 0.005, 0.001, 0.076, 0.017, 0.31, 0.005, 0.069, 0.037,
0.195), sig = c("*", "", "*", "*", "*", "*", "*", "*", "*",
"*", "*", "*", "*", "", "*", "", "*", "", "*", "")), row.names = c(NA,
-20L), class = "data.frame")
Plotting code:
p1 <-
sig_distance %>%
rename("Period"=period)%>%
filter(Period == "Before-to-during")%>%
mutate(group = factor(group))%>%
arrange(MPA_type, -value, group)%>%
ggplot(aes(x=reorder(MPA_type, -value), y=value, color=group,
#shape=group,
#fill=MPA_type
))+
geom_point(position = position_dodge(width=0.8),
size=3)+
geom_errorbar(aes(ymin=value-sd_pooled,
ymax = value+sd_pooled), stat="identity",
position = position_dodge(width=0.8), size=0.3, width=.3)+
#add significance level
geom_text(aes(label=sig), size=5, vjust=-0.01,
position = position_dodge(width=0.8),
show.legend = FALSE)+
ylab("Distance (Bray-Curtis)")+
xlab("Community")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"),
legend.key=element_blank())
One option would be to add a helper column to you dataset as the interaction of MPA_type and group for which you set the order using forcats::fct_inorder after you arranged your dataset according to your desired order. Thus helper column could then be mapped on the group aes:
library(ggplot2)
library(dplyr)
library(forcats)
sig_distance %>%
rename("Period" = period) %>%
filter(Period == "Before-to-during") %>%
mutate(group = factor(group)) %>%
arrange(MPA_type, -value, group) %>%
mutate(mpa_ordered = fct_inorder(paste(MPA_type, group, sep = "."))) |>
ggplot(aes(x = MPA_type, y = value, color = group, group = mpa_ordered)) +
geom_point(
position = position_dodge(width = 0.8),
size = 3
) +
geom_errorbar(
aes(
ymin = value - sd_pooled,
ymax = value + sd_pooled
),
stat = "identity",
position = position_dodge(width = 0.8), size = 0.3, width = .3
) +
geom_text(aes(label = sig),
size = 5, vjust = -0.01,
position = position_dodge(width = 0.8),
show.legend = FALSE
) +
ylab("Distance (Bray-Curtis)") +
xlab("Community") +
theme(
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"),
legend.key = element_blank()
)
I am trying to change the size of the strips. I want them smaller than what i am getting as they are taking a lot of space. I tried the code in this thread: edit strip size ggplot2. The recommendation is to use theme(strip.text.x = element_text(margin = margin(2,0,2,0, "cm"))). It did not work because i cannot merge it with theme(strip.text = element_blank()). I include the latter line of code because i do not want the name of the countries to show in the strips, i add them manually using geom_text. Is there a way to keep the name of the countries hidden in the strips but make the size of the strips smaller in height?
Here is the code below (the code is long because i add manually a legend to the graph):
library(gtable)
library(gridExtra)
library(ggplot2)
p1= df %>%
ggplot(aes(x= estimate, y=term)) +
geom_text(aes(label = cntry, x = Inf, y = Inf), vjust = 1.75, hjust =1)+
geom_point(mapping=aes(x=estimate, y=term)) +
facet_wrap(vars(cntry,continent))
#Plot it
dummy= df %>%
ggplot(aes(x = estimate, y = term))+
facet_wrap(vars(cntry,continent)) +
geom_rect(aes(fill=continent), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
theme_minimal() +
theme(strip.text = element_blank())
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(dummy)
gtable_select <- function (x, ...)
{
matches <- c(...)
x$layout <- x$layout[matches, , drop = FALSE]
x$grobs <- x$grobs[matches]
x
}
panels <- grepl(pattern="panel", g2$layout$name)
strips <- grepl(pattern="strip-t", g2$layout$name)
g2$layout$t[panels] <- g2$layout$t[panels] - 1
g2$layout$b[panels] <- g2$layout$b[panels] - 1
new_strips <- gtable_select(g2, panels | strips)
grid.newpage()
grid.draw(new_strips)
gtable_stack <- function(g1, g2){
g1$grobs <- c(g1$grobs, g2$grobs)
g1$layout <- transform(g1$layout, z= z-max(z), name="g2")
g1$layout <- rbind(g1$layout, g2$layout)
g1
}
new_plot <- gtable_stack(g1, new_strips)
grid.newpage()
grid.draw(new_plot)
# Extract only the legend from "dummy" plot
g_legend <- function(dummy){
tmp <- ggplot_gtable(ggplot_build(dummy))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}
# Assign the legend to a separate object
facet.legend <- g_legend(dummy)
jpeg("legend_graph.jpg", width = 8, height = 6, units = "in", res = 300)
print(grid.arrange(new_plot, facet.legend, nrow = 2, widths = c(7, 1), heights = c(6, 0.01)))
dev.off()
Here is the data:
structure(list(cntry = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("China",
"USA", "Germany", "Canada"), class = "factor"), term = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L), .Label = c("2008", "2009", "2010", "2011", "2012",
"2013", "2014", "2015", "2016", "2017"), class = "factor"), estimate = c(-0.02,
-0.02, -0.05, -0.01, 0, 0.02, -0.01, 0.03, 0.02, 0, 0.09, 0.1,
0.04, 0.13, 0.1, 0.07, 0.08, 0.01, -0.1, -0.09, -0.1, -0.1, -0.13,
-0.12, -0.18, -0.07, -0.04, 0.01, 0, -0.02, -0.02, 0.03, 0.01,
0.01, 0.03, 0.03), continent = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Asia",
"America", "Europe"), class = "factor")), row.names = c(NA, -36L
), class = "data.frame")
I am using ezPlot from the ez package in R to plot results of a mixed within and between-ss design. The data point from the two groups I have overlap so that I would like to jitter both the data point and associated error bar.
data<-structure(list(Sub = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("WW", "XX", "YY",
"ZZ"), class = "factor"), DepVar = c(0.67, 0.35, 0.09, 0.2, 0.19,
0.13, 0.45, 0.23, 0.08, 0.32, 0.17, 0.18, 0.67, 0.36, 0.55, 0.4,
0.37, 0.05, 0.26, 0.11, 0.08, 0.46, 0.29, 0.18, 0.16, 0, 0.38,
0.22, 0.08, 0.1, 0.54, 0.17, 0.07, 0.38, 0.75, 0.87, 0.27, 0.57,
0.31, 0.28, 0.07, 0.12, 0.75, 0.33, 0.23, 0.33, 0.26, 0.18),
Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"),
Con = structure(c(1L, 3L, 3L, 3L, 4L, 5L, 2L, 3L, 4L, 1L,
2L, 3L, 1L, 3L, 3L, 3L, 4L, 5L, 2L, 3L, 4L, 1L, 2L, 3L, 1L,
3L, 3L, 3L, 4L, 5L, 2L, 3L, 4L, 1L, 2L, 3L, 1L, 3L, 3L, 3L,
4L, 5L, 2L, 3L, 4L, 1L, 2L, 3L), .Label = c("C", "D", "E",
"F", "G"), class = "factor")), .Names = c("Sub", "DepVar",
"Group", "Con"), class = "data.frame", row.names = c(NA, -48L))
ezPlot( data,
dv = .(DepVar),
wid = .(Sub), # subject
within = .(Con),
between=.(Group),
split=.(Group),
do_bars=TRUE,
type = 2,
x = .(Con))
A non elegant trick is so set scale-color manual white so that the underlying data points disappear and then using geom-point position dodge(0.4))
ezPlot( data,
dv = .(DepVar),
wid = .(Sub), # subject
within = .(Con),
between=.(Group),
split=.(Group),
do_bars=TRUE,
type = 2,
x = .(Con))+
scale_color_manual(values=c("white", "white"))+
geom_point(aes(fill=Group), color="black", pch= 21, size= 3, position=position_dodge(0.4))+
geom_line(aes(group = Group), lty = 3, lwd = 1.3, color='black')
however, I would like to have the error bar plotted and I don't know how to achieve this or if other workarounds are possible. I would like to stick to ezplot. Thanks!
One way is to use set print_code = TRUE, to produce data to be plotted, as well as the ggplot code:
library(ggplot2)
stats <- ezPlot( data,
dv = .(DepVar),
wid = .(Sub), # subject
within = .(Con),
between=.(Group),
split=.(Group),
do_bars=TRUE,
type = 2,
x = .(Con),
print_code = TRUE)
Then, manually modify the code to add position = position_dodge(0.4) to each geom, then run the ggplot code.
A more efficient way to do the same thing would be to capture.output the code as a character vector, use gsub to add position = position_dodge(0.4), then eval(parse(text = ...)) the modified code:
gg_code <- capture.output(stats <- ezPlot( data,
dv = .(DepVar),
wid = .(Sub), # subject
within = .(Con),
between=.(Group),
split=.(Group),
do_bars=TRUE,
type = 2,
x = .(Con),
print_code = TRUE))
gg_code <- gsub("alpha", "position = position_dodge(0.4), alpha", gg_code)
eval(parse(text = paste(gg_code, collapse = "")))
Output:
I am using facet_grid to plot my data. I have three plots in grid and I Want to fix the ymax (or ylimit) for two of the plots. Currently, I am using following code
f <- ggplot(data=newmel,aes(x=timestamp,y=value,ymin=0,ymax=value))+facet_grid(variable~., scales = "free_y")+
theme(axis.title.x=element_blank(),axis.title.y=element_blank())
f1 <- f + geom_linerange(subset=.(variable=="hpanom")) # require(plyr) for dot function
f2 <- f1 + geom_linerange(subset=.(variable=="lofanom"))
f3 <- f2 + geom_line(subset=.(variable=="power"))
f3
The output plot is:
I want to fix the range of first two (hpanom and lofanom) plots from 0-1 and I do not care for the third one. This is because first two represent probabilites, hence range is always fixed, whereas the third one represent values of which I do not know limits.
Here I am attaching my whole dataset,so that it become easy to replicate the case
structure(list(timestamp = structure(c(1438450200, 1438536600,
1438623000, 1438709400, 1438795800, 1438882200, 1438968600, 1439055000,
1439141400, 1439227800, 1439314200, 1439400600, 1439487000, 1439573400,
1439659800, 1439746200, 1439832600, 1439919000, 1440005400, 1440091800,
1440178200, 1440264600, 1440351000, 1440437400, 1440523800, 1440610200,
1440696600, 1440783000, 1440869400, 1440955800, 1438450200, 1438536600,
1438623000, 1438709400, 1438795800, 1438882200, 1438968600, 1439055000,
1439141400, 1439227800, 1439314200, 1439400600, 1439487000, 1439573400,
1439659800, 1439746200, 1439832600, 1439919000, 1440005400, 1440091800,
1440178200, 1440264600, 1440351000, 1440437400, 1440523800, 1440610200,
1440696600, 1440783000, 1440869400, 1440955800, 1438450200, 1438536600,
1438623000, 1438709400, 1438795800, 1438882200, 1438968600, 1439055000,
1439141400, 1439227800, 1439314200, 1439400600, 1439487000, 1439573400,
1439659800, 1439746200, 1439832600, 1439919000, 1440005400, 1440091800,
1440178200, 1440264600, 1440351000, 1440437400, 1440523800, 1440610200,
1440696600, 1440783000, 1440869400, 1440955800), tzone = "Asia/Kolkata", tclass = c("POSIXct",
"POSIXt"), class = c("POSIXct", "POSIXt")), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("power", "hpanom",
"lofanom"), class = "factor"), value = c(713.818544290426, 1.60000010638915e-16,
1588.93456060134, 1080.34496835479, 1602.88616015399, 1325.85664208325,
1487.27242035101, 647.925289288333, 1.60000010638915e-16, 1280.71707200196,
1558.88686231823, 1349.32481298453, 1386.23617766603, 995.841940511863,
961.865845980269, 1153.33076383446, 1601.85509748887, 1346.15843354498,
1209.13461354976, 1060.91957740428, 1963.3986755642, 1995.90120364349,
1.60000010638915e-16, 1391.2765167008, 1198.11633766185, 1202.33001076712,
1508.21685464222, 1299.09592097037, 1.60000010638915e-16, 1.60000010638915e-16,
0.718682639785718, 0.835994256774337, 0.323313580710195, 0.323826587878846,
0.252277897807753, 0.427692277575098, 0, 0.737219412602466, 0.835994256774337,
0.165199776286282, 0.0893388057418736, 0.20964070918484, 0.13145481071022,
0.408292163070401, 0.824540692174425, 0.71239540630304, 0.323313580710195,
0.485828441524218, 0.188258288292337, 0.231165659725455, 0.907512281748878,
0.951462340841186, 0.835994256774337, 0.29824669841755, 0.245223685520087,
0.108813825489535, 0.387339161244294, 0.359556716396615, 0.835994256774337,
0.835994256774337, 0.4, 0.43, 0.13, 0.28, 0.11, 0.1, 0.13, 0.43,
0.43, 0.13, 0.11, 0.01, 0.12, 0.3, 0.6, 0.4, 0.2, 0.11, 0.1,
0.15, 0.67, 1, 0.43, 0.08, 0.08, 0.12, 0.07, 0.08, 0.43, 0.43
)), row.names = c(NA, -90L), .Names = c("timestamp", "variable",
"value"), class = "data.frame")
It's a bit of a hack, but you can plot an "invisible" point by using size=0 at y=1 to force the limit to 1 on the two linerange plots as shown below. I've also removed need to use plyr.
f <- ggplot(data=newmel,aes(x=timestamp,y=value,ymin=0,ymax=value))+facet_grid(variable~., scales = "free_y")
f <- f + theme(axis.title.x=element_blank(),axis.title.y=element_blank())
f <- f + geom_linerange(data=subset(newmel, variable %in% c("hpanom","lofanom")))
# plot invisible point ( size=0) to set upper limit of y axis to 1
f <- f + geom_point(data=subset(newmel, variable %in% c("hpanom","lofanom")),
aes(x=min(timestamp), y=1), size=0)
f <- f + geom_line(data=subset(newmel, variable=="power"))
f
Update for ggplot 2.0
In ggplot 2.0, setting size=0 no longer makes the point invisible. Instead, use colour = NA to make it transparent . New solution is
library(ggplot2)
f <- ggplot(data=newmel,aes(x=timestamp,y=value,ymin=0,ymax=value))+facet_grid(variable~., scales = "free_y")
f <- f + theme(axis.title.x=element_blank(),axis.title.y=element_blank())
f <- f + geom_linerange(data=subset(newmel, variable %in% c("hpanom","lofanom")))
# plot transparent point ( colour = NA) to set upper limit of y axis at 1
f <- f + geom_point(data=subset(newmel, variable %in% c("hpanom","lofanom")),
aes(x=timestamp[1], y=1), colour=NA )
f <- f + geom_line(data=subset(newmel, variable=="power"))
f