Is it possible to get data from a plot in R - 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")

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))

Plotting PCA, autoplot() doesn't separate colors by group variable

I am using ggplot2 package and ggfortify to plot PCA results. The last column of my data matrix is a column of four different factors. Name of the column is 'group'.
It is like:
group
a
b
a
c
d
The code I used is:
autoplot(prcomp(df), data = mydata, colour = "group",frame=T)
However, in the plot generated, the groups of different points are not separated by color.
enter image description here
Any advices? Thank you in advance.
Problem solved, the grouping variable need to be formatted as a Factor, instead of a column of characters.

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()

Multiple plots factor by ID and Day

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)

ggplot multiple grouping bar

I would like to know how to get 9 grouping bar plot (3x3) together.
My CSV:
data <- read.csv("http://pastebin.com/raw.php?i=6pArn8GL", sep = ";")
The 9 plots should be grouped according "Type" A to I.
Then each grouped bar plot should have the frequency on the y axis, the x axis is grouped by 1 pce to 6 pce and subdivided by year.
I have the following example on Excel (cf. image) and would like to create the same result on r with ggplot. Is it possible?
First, reshape your data from wide to long format.
library(reshape2)
df.long<-melt(df,id.vars=c("ID","Type","Annee"))
Next, as during importing data letter X is added to variable names starting with number, remove it with substring().
df.long$variable<-substring(df.long$variable,2)
Now use variable as x, value as y, Annee for fill and geom_bar() to get barplot. With facet_wrap() you can split data by Type.
ggplot(df.long,aes(variable,value,fill=as.factor(Annee)))+
geom_bar(position="dodge",stat="identity")+
facet_wrap(~Type,nrow=3)
Using #Didzis reshaped data , here a lattice version:
barchart(value~variable|Type,
groups=Annee,data=df.long,layout=c(3,3),
between=list(3,3),
axis=axis.grid,
auto.key=TRUE)

Resources