Multiple plots factor by ID and Day - r

Hi I am trying to plot multiple plots factor by ID and DAY. Each ID will have multiple plots based on the day, all ID's have multiple day data so multiple plots. I tried with the lattice plot as shown below. But factor with both day and ID is an issue.
library("lattice")
# require("lattice") - you do not need this line
xyplot(IPRE+PRED+DV) ~ TIME| ID, data= df ,type=c("l","l","p"),col= c("blue","black","red"),
distribute.type=TRUE, xlab="Time (h)",ylab="conc",layout=c(0,4))
Columns ID DAY TIME DV IPRED PRED

Not too sure what your ultimate goal is but this may be of some assistance. facet_wrap from the ggplot package allows you to split the plots by multiple variables.
library(ggplot2)
data(iris)
iris$Day<-rep(weekdays(Sys.Date()+0:4),each=10)
ggplot(data=iris,aes(x=Sepal.Width,y=Sepal.Length))+
geom_point(aes(colour=Species))+
facet_wrap(~Day+Species,nrow=5)

Related

Graphing different variables in the same graph R- ggplot2

I have several datasets and my end goal is to do a graph out of them, with each line representing the yearly variation for the given information. I finally joined and combined my data (as it was in a per month structure) into a table that just contains the yearly means for each item I want to graph (column depicting year and subsequent rows depicting yearly variation for 4 different elements)
I have one factor that is the year and 4 different variables that read yearly variations, thus I would like to graph them on the same space. I had the idea to joint the 4 columns into one by factor (collapse into one observation per row and the year or factor in the subsequent row) but seem unable to do that. My thought is that this would give a structure to my y axis. Would like some advise, and to know if my approach to the problem is effective. I am trying ggplot2 but does not seem to work without a defined (or a pre defined range) y axis. Thanks
I would suggest next approach. You have to reshape your data from wide to long as next example. In that way is possible to see all variables. As no data is provided, this solution is sketched using dummy data. Also, you can change lines to other geom you want like points:
library(tidyverse)
set.seed(123)
#Data
df <- data.frame(year=1990:2000,
v1=rnorm(11,2,1),
v2=rnorm(11,3,2),
v3=rnorm(11,4,1),
v4=rnorm(11,5,2))
#Plot
df %>% pivot_longer(-year) %>%
ggplot(aes(x=factor(year),y=value,group=name,color=name))+
geom_line()+
theme_bw()
Output:
We could use melt from reshape2 without loading multiple other packages
library(reshape2)
library(ggplot2)
ggplot(melt(df, id.var = 'year'), aes(x = factor(year), y = value,
group = variable, color = variable)) +
geom_line()
-output plot
Or with matplot from base R
matplot(as.matrix(df[-1]), type = 'l', xaxt = 'n')
data
set.seed(123)
df <- data.frame(year=1990:2000,
v1=rnorm(11,2,1),
v2=rnorm(11,3,2),
v3=rnorm(11,4,1),
v4=rnorm(11,5,2))

Is it possible to get data from a plot in R

I have this plot where I plotted patient ID's on the x axis and BMI on the y axis. I found a cluster of a data in "severely underweight" category as u can see in the plot. How can I get a table of all those points which are in here?
OR
How can I extract one category from a column in R.
Assuming that
your data is the d1 data frame
the category is given by the group column
Then one possibility is to use dplyr package:
suw <- filter(d1, group == "Severely underweight")

Data from two data frames in one plot (R)

I've got two data frames in R, both of the same structure - with columns named: Year, Age, Gender and Value1.
What I'd like to do, is to plot (as points) Value1 (on Y axis) against Year (on X axis), for a particular gender and age. The plot should consists of points from both data frames (with legend indicating which points are from which data frame).
What I've done is:
attach(df1)
plot(Value1[Gender=="Female" & Age==30] ~ Year[Gender=="Female" & Age==30])
which creates the plot with points from one data frame. The question is, how to add the points from the second data frame to the same plot, and how to create proper legend? I tried few combinations of the points() formula, but it did not help.
without a reproducable example it is not very easy to help. Assuming your data frames are called df1,df2 you can try this:
library(ggplot2)
library(dplyr)
df1$frame="1"
df2$frame="2"
df=rbind(df1,df2)
df<-filter(df,Gender=="Female"&Age==30)
ggplot(data=df,aes(x=Year,y=Value1,col=frame))+geom_point()

Box-and-Whisker plot grouped by year using R

I have timeseries object in R which contains the values of AirPassenger bookings in every month from year 1949-1960. Its easy to plot box plot grouped by month using the command boxplot(AP ~ cycle(AP)). I would like to know how to do box plot if we have to group by year.
Sample codes to get you started:
ap <- data.frame(AirPassengers)
year <- rep(seq(1949,1960), rep(12,12))
boxplot(ap$AirPassengers~year)

How to plot weighted means by group?

I am looking for a straightforward way to plot weighted means by group on a line chart (each line representing a country, example provided below).
My data has a hierarchical structure where individuals nested in countries and countries nested in years, but not all countries have surveys in each year. Here is my attempt to create a minimal data frame similar to my data:
dat <- data.frame(country=rep(LETTERS, each=3)[1:20], replicate(3, sample(11, 10)), year=sample(c(1990:2000), 100, TRUE),wght=sample(c(1:3), 100, TRUE))
I managed to calculate weighted.means by country-year with the following code and I saved it as a matrix:
mat1<-by(dat,list(dat$country,dat$year), function(x)with(x,weighted.mean(x$X1,x$wght)))[]
But this is how far I could get. Can somebody please help me with plotting the weighted averages by country-year from this matrix or offer an alternative way to plot weighted averages by groups?
To be perfectly clear, this is how I need my graph to look like:
Use :
plyr or data.table to aggregate your data
lattice or ggplot2 to plot by group
For example using plyr/lattice:
library(plyr)
mat.mean <- ddply(dat,.(country,year),summarise, value=weighted.mean(X1,wght))
library(lattice)
xyplot(value~year,groups=country,
type='l',data=mat.mean,
auto.key =list(columns = 3))

Resources