I try to make ten lines using ggplot2, the data is from a 15*10 matrix and I want to plot line 1 using the first column of matrix, line 2 using the second column of matrix, etc. My code looks like this, but the output is really weird.
The code is:
prop_df <- data.frame(prop_combined)
colnames(prop_df) <- c('Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6', 'Q7', 'Q8', 'Qinf', 'Qhyb')
prop_df <- stack(as.data.frame(prop_combined))
prop_df$x <- (rep(seq_len(nrow(prop_combined)), ncol(prop_combined))-1)/10
ggplot(data = prop_df, aes(x = x, y = values), group = ind) +
geom_line() +
geom_point() +
labs(color='Gamma') +
ylim(0, 80) +
xlab(TeX("$tau$")) +
ylab("Power")
The prop_df looks like:
values ind x
1 8.6 Q1 0.0
2 10.7 Q1 0.1
3 11.8 Q1 0.2
4 11.2 Q1 0.3
5 13.0 Q1 0.4
6 15.6 Q1 0.5
7 21.4 Q1 0.6
8 25.0 Q1 0.7
9 28.8 Q1 0.8
10 34.2 Q1 0.9
11 39.5 Q1 1.0
12 48.2 Q1 1.1
13 55.2 Q1 1.2
14 61.6 Q1 1.3
15 67.2 Q1 1.4
16 71.7 Q1 1.5
17 8.8 Q2 0.0
18 11.0 Q2 0.1
19 10.7 Q2 0.2
And the output is:
In addition to #Freguglia's suggestions, I made some changes to create a clear plot. I used the Cairo() library to name the x axis.
Sample code:
library(ggplot2)
library(Cairo) # to define the greek letters
prop_df <- data.frame(prop_df)
#colnames(prop_df) <- c('Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6', 'Q7', 'Q8', 'Qinf', 'Qhyb') # for this example you don't need this line
#prop_df <- stack(as.data.frame(prop_combined)) # this was already
#prop_df$x <- (rep(seq_len(nrow(prop_combined)), ncol(prop_combined))-1)/10
prop_df$values=factor(prop_df$values, levels=c("8.6","8.8", "11", "10.7", "11.2", "11.8", "13", "15.6" ,"21.4" ,"25", "28.8", "34.2", "39.5", "48.2", "55.2", "61.6", "67.2", "71.7"))
prop_df$x=factor(prop_df$x, levels=c("0", "0.1","0.2","0.3","0.4","0.5","0.6","0.7","0.8","0.9","1","1.1","1.2","1.3","1.4","1.5"))
ggplot(data = prop_df, aes(x = x, y = values, group = ind)) +
geom_line(lwd=2) +
geom_point(aes(color=ind), size=6) +
labs( y="Power",color="Conditions", x=paste(expression("\u03A4"))) +
theme_minimal()
Plot:
Sample data:
values=c(8.6,10.7, 11.8, 11.2, 13.00,15.6,21.4, 25.00, 28.8,34.2, 39.5, 48.2, 55.2,61.6,67.2, 71.7,8.8, 11.00, 10.7)
x=c(0, 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,1.1,1.2,1.3,1.4,1.5, 0.00, 0.1, 0.2)
ind=c("Q1","Q1","Q1","Q1","Q1","Q1","Q1","Q1","Q1","Q1","Q1","Q1","Q1","Q1","Q1","Q1","Q2","Q2","Q2")
prop_df<-cbind(values, ind,x)
Related
I did some non-linear regression and retrieved the fitting coefficients/parameters b0,b1,b2, and b3 in a data frame df. I would like to plot the same function with parameters from each row as an overlay with the initial data points (from df2) that I have used for fitting. All contained in one single graph. Any help is appreciated. Thank you in advance!
group
b0
b1
b2
b3
abc
6.3
8.9
1.66
0.025
def
5.1
8.9
1.48
1.27
ghi
5.5
9.0
1.41
1.03
group<-c("abc","def","ghi")
b0 <- c(6.3,5.1,5.5)
b1 <- c(8.9,8.9,9.0)
b2 <- c(1.66,1.48,1.41)
b3 <- c(0.025,1.27,1.03)
df <- data.frame(group,b0, b1, b2,b3)
I have tried to apply the solution from other posts but couldn't make it work.
The following just connects the points of all 3 functions without distinguishing between the group, as it does for geom_point()
output
f <- function(x, b0,b1,b2,b3) b0*exp(-0.5*((x-b1)/b2)^2) + b3
ggplot(df2, aes(x = x, y = y, color=group))+
geom_point() +
stat_function(fun = f, args = list(b0 = df$b0, b1 = df$b1, b2 = df$b2, b3 = df$b3))
And the solution from here approach 2 returns
1: Computation failed in stat_function(): non-numeric argument to
binary operator
coeflines <-
alply(as.matrix(df), 1, function(df) {
stat_function(fun=function(x){df[2]*exp(-0.5*((x-df[3])/df[4])^2) + df[5]}, colour="grey")
})
ggplot(df2, aes(x=x, y=y, color=group)) +
scale_x_continuous(limits=c(0,15)) +
scale_y_continuous(limits=c(0,15)) +
coeflines +
geom_point()
Thank you for your help!
Making some assumptions about the value of x here. It looks like you're trying to use stat_function in a way that isn't intended. It doesn't handle grouping and isn't meant to take a data set as input, so you would need repeated calls for each set of parameters. Instead, we can just evaluate the function for each set of variables, store the results, and plot those. I'm also sampling a few points from each function to simulate your request to put the original data on the plot as well.
library(tidyverse)
f <- function(x, b0,b1,b2,b3) b0*exp(-0.5*((x-b1)/b2)^2) + b3
df.func <- df %>%
group_by(group, b0, b1, b2, b3) %>%
summarize(
x = seq(0, 20, length = 100),
y = f(x, b0, b1, b2, b3)
)
# A tibble: 300 × 7
# Groups: group, b0, b1, b2 [3]
group b0 b1 b2 b3 x y
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 abc 6.3 8.9 1.66 0.025 0 0.0250
2 abc 6.3 8.9 1.66 0.025 0.202 0.0250
3 abc 6.3 8.9 1.66 0.025 0.404 0.0250
4 abc 6.3 8.9 1.66 0.025 0.606 0.0250
5 abc 6.3 8.9 1.66 0.025 0.808 0.0250
6 abc 6.3 8.9 1.66 0.025 1.01 0.0251
7 abc 6.3 8.9 1.66 0.025 1.21 0.0251
8 abc 6.3 8.9 1.66 0.025 1.41 0.0252
9 abc 6.3 8.9 1.66 0.025 1.62 0.0254
10 abc 6.3 8.9 1.66 0.025 1.82 0.0257
# … with 290 more rows
df.points <- df.func %>%
sample_n(10)
ggplot(df.func, aes(x = x, y = y, color = group))+
geom_line() +
geom_point(data = df.points)
I'd like to make a boxplot with mean instead of median. Moreover, I would like the line to stop at 5% (lower) end 95% (upper) quantile. Here the code;
ggplot(data, aes(x=Cement, y=Mean_Gap, fill=Material)) +
geom_boxplot(fatten = NULL,aes(fill=Material), position=position_dodge(.9)) +
xlab("Cement") + ylab("Mean cement layer thickness") +
stat_summary(fun=mean, geom="point", aes(group=Material), position=position_dodge(.9),color="black")
I'd like to change geom to errorbar, but this doesn't work. I tried middle = mean(Mean_Gap), but this doesn't work either. I tried ymin = quantile(y,0.05), but nothing was changing. Can anyone help me?
The standard boxplot using ggplot. fill is Material:
Here is how you can create the boxplot using custom parameters for the box and whiskers. It's the solution shown by #lukeA in stackoverflow.com/a/34529614/6288065, but this one will also show you how to make several boxes by groups.
The R built-in data set called "ToothGrowth" is similar to your data structure so I will use that as an example. We will plot the length of tooth growth (len) for each vitamin C supplement group (supp), separated/filled by dosage level (dose).
# "ToothGrowth" at a glance
head(ToothGrowth)
# len supp dose
#1 4.2 VC 0.5
#2 11.5 VC 0.5
#3 7.3 VC 0.5
#4 5.8 VC 0.5
#5 6.4 VC 0.5
#6 10.0 VC 0.5
library(dplyr)
# recreate the data structure with specific "len" coordinates to plot for each group
df <- ToothGrowth %>%
group_by(supp, dose) %>%
summarise(
y0 = quantile(len, 0.05),
y25 = quantile(len, 0.25),
y50 = mean(len),
y75 = quantile(len, 0.75),
y100 = quantile(len, 0.95))
df
## A tibble: 6 x 7
## Groups: supp [2]
# supp dose y0 y25 y50 y75 y100
# <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 OJ 0.5 8.74 9.7 13.2 16.2 19.7
#2 OJ 1 16.8 20.3 22.7 25.6 26.9
#3 OJ 2 22.7 24.6 26.1 27.1 30.2
#4 VC 0.5 4.65 5.95 7.98 10.9 11.4
#5 VC 1 14.0 15.3 16.8 17.3 20.8
#6 VC 2 19.8 23.4 26.1 28.8 33.3
# boxplot using the mean for the middle and 95% quantiles for the whiskers
ggplot(df, aes(supp, fill = as.factor(dose))) +
geom_boxplot(
aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
stat = "identity"
) +
labs(y = "len", title = "Boxplot with Mean Middle Line") +
theme(plot.title = element_text(hjust = 0.5))
In the figure above, the boxplot on the left is the standard boxplot with regular median line and regular min/max whiskers. The boxplot on the right uses the mean middle line and 5%/95% quantile whiskers.
I want to specify which column I want as label and value in a pie chart
The problem is when I use the function hc_add_series_labels_values() which accept this 2 argument I have no output because seems to be deprecated.
The hc_add_series() seems to automaticly the 2 column depending on order, type ...
This package is not well documented I couldnt find what I need
Thanks
In my example I want to specify the name2 column as label
and high as value, how to do that ?
library(dplyr)
library(highcharter)
n <- 5
set.seed(123)
colors <- c("#d35400", "#2980b9", "#2ecc71", "#f1c40f", "#2c3e50", "#7f8c8d")
colors2 <- c("#000004", "#3B0F70", "#8C2981", "#DE4968", "#FE9F6D", "#FCFDBF")
df <- data.frame(x = seq_len(n) - 1) %>%
mutate(
y = 10 + x + 10 * sin(x),
y = round(y, 1),
z = (x*y) - median(x*y),
e = 10 * abs(rnorm(length(x))) + 2,
e = round(e, 1),
low = y - e,
high = y + e,
value = y,
name = sample(fruit[str_length(fruit) <= 5], size = n),
color = rep(colors, length.out = n),
segmentColor = rep(colors2, length.out = n)
)
df$name2 <- c("mos", "ok", "kk", "jji", "hufg")
## x y z e low high value name color segmentColor
## 1 0 10.0 -25.6 7.6 2.4 17.6 10.0 plum #d35400 #000004
## 2 1 19.4 -6.2 4.3 15.1 23.7 19.4 lemon #2980b9 #3B0F70
## 3 2 21.1 16.6 17.6 3.5 38.7 21.1 mango #2ecc71 #8C2981
## 4 3 14.4 17.6 2.7 11.7 17.1 14.4 pear #f1c40f #DE4968
## 5 4 6.4 0.0 3.3 3.1 9.7 6.4 apple #2c3e50 #FE9F6D
highchart() %>%
hc_chart(type = "pie") %>%
hc_add_series(df, name = "Fruit Consumption", showInLegend = FALSE)
For People who have same problem you can check this :
This package seems to work like ggplot2, the function hchart do the job with the hcaes argument
hchart(df, type = "pie", hcaes(name2, high))
Output :
I have created a dummy dataframe representative of my data-
SQ AgeGroup Prop LCI UCI
2010-1 0 to 18 4.3 4.2 4.4
2010-1 19 to 25 5.6 5.3 5.6
2010-1 26 and over 7.8 7.6 7.9
2010-2 0 to 18 4.1 3.9 4.2
2010-2 19 to 25 5.8 5.6 5.9
2010-2 26 and over 8.1 7.9 8.3
2010-3 0 to 18 4.2 4 4.4
2010-3 19 to 25 5.5 5.2 5.6
2010-3 26 and over 7.6 7.4 7.7
2010-4 0 to 18 3.9 3.6 4.1
2010-4 19 to 25 5.2 5 5.4
2010-4 26 and over 7.4 7.2 7.6
2011-1 0 to 18 4.3 4.1 4.5
2011-1 19 to 25 5.7 5.5 5.8
2011-1 26 and over 8.2 8 8.3
2011-2 0 to 18 4.1 4 4.5
2011-2 19 to 25 5.7 5.5 5.9
2011-2 26 and over 8.2 8 8.4
2011-3 0 to 18 4.4 4.2 4.6
2011-3 19 to 25 5.7 5.5 7.9
2011-3 26 and over 8.2 8 8.4
which creates an image that looks like this-
I have used the following code-
library(readxl)
library(dplyr)
library(epitools)
library(gtools)
library(reshape2)
library(binom)
library(pivottabler)
library(readxl)
library(phecharts)
library(ggplot2)
library(RODBC)
rm(list=ls())
df<-read_xlsx("Dummydata.xlsx")
pd<-position_dodge(width=0.3)
limits <- aes(ymax =df$UCI , ymin = df$LCI)
p<-ggplot(df, aes(x = SQ, y =Prop, group=AgeGroup, colour= AgeGroup)) +
geom_line(position=pd)+
geom_point(size=2.0, position=pd)+
geom_errorbar(limits, width = 0.55, size=0.4, position= pd)+
labs(
y = "Percentage",
x = "Study Quarter")
p<-p +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
scale_y_continuous(name="Percentage",breaks=c(0,2,4,6,8,10),limits=c(0,10))+#limits need to change with every pot
scale_fill_manual(values = pal)+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1,size=16))+
theme(axis.text.y=element_text(size=16))+
theme(legend.text = element_text(size=18))+
theme(legend.title=element_text(size=16))+
theme(legend.title=element_blank())+
theme(legend.position="bottom")+
theme(axis.title = element_text(size=22))
p + geom_vline(xintercept = c(2,4,6), linetype="dotted",
color = "black", size=1.0, show.legend = TRUE)
However, what I want is that the three geom lines should have a lable (L1, L2 and L3) at the top of each of these lines and a separate legend at the bottom where I can add what these lines stand for. Something like this-
L1: Launch of x
L2: Launch of y
L3: Launch of z
Can someone please help with this?
I'm trying to plot a geom_tile plot for a dataset, where I need to highlight the max and min values in every row (colour palette going from green to red)
Dataset:
draft_mean trim rf_pwr
1 12.0 1.0 12253
2 12.0 0.8 12052
3 12.0 0.6 12132
4 12.0 0.4 12280
5 12.0 0.2 11731
6 12.0 0.0 11317
7 12.0 -0.2 12126
8 12.0 -0.4 12288
9 12.0 -0.6 12461
10 12.0 -0.8 12791
11 12.0 -1.0 12808
12 12.2 1.0 12346
13 12.2 0.8 12041
14 12.2 0.6 12345
15 12.2 0.4 12411
16 12.2 0.2 12810
17 12.2 0.0 12993
18 12.2 -0.2 12796
19 12.2 -0.4 12411
20 12.2 -0.6 12342
21 12.2 -0.8 12671
22 12.2 -1.0 13161
ggplot(dataset, aes(trim, draft_mean)) +
geom_tile(aes(fill=rf_pwr), color="black") +
scale_fill_gradient(low= "green", high= "red") +
scale_x_reverse() +
scale_y_reverse()
This plot (image) is taking the minimum values and plotting them as green and maximum values as red. What I need help with is that I need colour palette to go from green to red (minimum to maximum) for every row of the plot (2 rows in this plot) rather than the whole plot.
For draft_mean=12.2, rf_pwr should be colour formatted from minimum to maximum for trim values.
For every value of draft_mean, I should be able to tell the trim values with lowest and highest rf_pwr.
I can plot individual draft_mean values to check, but all draft_mean values needs to be visualized together.
You can create a scaled variable where min = 0 and max = 1 per group like this:
require(tidyverse)
# create toy data
set.seed(1)
df <- data.frame(
draft_mean =sort(rep(c(12,12.2),11 )),
trim=rep(sample(seq(-1,1,length.out = 11), replace = F),2),
rf_pwr = sample(11000:13000,22)
)
# create a scaled variable per unique draft_mean (min = 0 and max = 1)
df <- df %>% group_by(draft_mean) %>% mutate(rf_scl = (rf_pwr-
min(rf_pwr))/(max(rf_pwr)-min(rf_pwr)))
ggplot(df, aes(trim, draft_mean)) +
geom_tile(aes(fill=rf_scl), color="black") +
scale_fill_gradient(low= "green", high= "red") +
scale_x_reverse() +
scale_y_reverse()