I have created two clusters while I used the code to visualize the clusters through line chart, it shows "Each group consists of only one observation. Do you need to adjust the group aesthetic?"
Code
ggplot(b,aes(A, avg, colour=cluster))+geom_line()
Dataset
A cluster avg
A1 1 0.2
A1 2 0.3
A2 1 0.3
A2 2 0.4
b <- data.frame(A=c("A1","A2","A3"),
cluster=c(1,2,1),
avg=c(0.2,0.3,0.3))
x11()
ggplot(b,aes(A, avg, colour=cluster))+geom_point()
You only have one point so don't use lines, group aesthetics is explained here http://ggplot2.tidyverse.org/reference/aes_group_order.html
Related
I am trying to plot an "assignability plot" in which the probability of belonging to three groups is shown on a triangle with dot as each observation. The center of the triangle will represent observations with equal probability (which is 33%) to belonging to each of the three groups (Group1_prob, Group2_Prob, Group3_Prob), and other areas in the triangle will represent varying degrees of group membership. These observations or dots have colours according to Another_category.
I have generated a random dataset with variables I have mentioned above to clarify:
s <- seq(1, 100, 1)
a1 <- matrix(rbeta(100*3,2,2), nc=3)
a1 <- sweep(a1, 1, rowSums(a1), FUN="/")
category2 <- sample(c(1,2,3), 100, replace = TRUE)
df = data.frame(s, a1, category2)
colnames(df) <- c('observation', 'Group1_prob', 'Group2_prob', 'Group3_prob', 'Another_category')
The data frame will look something like the below:
observation Group1_prob Group2_prob Group3_prob Another_category
1 0.20692290 0.5259100 0.2671671 1
2 0.32271247 0.4352754 0.2420121 3
3 0.26894997 0.2367609 0.4942891 2
4 0.51197553 0.2400177 0.2480067 3
5 0.29448485 0.3002781 0.4052370 2
6 0.39686890 0.1370191 0.4661120 2
7 0.33881746 0.2946256 0.3665570 3
8 0.36083040 0.3123024 0.3268672 1
9 0.05739799 0.1207381 0.8218639 3
Would this be at all possible with ggplot2 in R?
A Nature Communications paper by Young et al shows this plot perfectly:
I have data that contains information about sub-plots with different numbers and their corresponding species types (more than 3 species within each subplot). Every species have X & Y coordinates.
> df
subplot species X Y
1 1 Apiaceae 268675 4487472
2 1 Ceyperaceae 268672 4487470
3 1 Vitaceae 268669 4487469
4 2 Ceyperaceae 268665 4487466
5 2 Apiaceae 268662 4487453
6 2 Magnoliaceae 268664 4487453
7 3 Magnoliaceae 268664 4487453
8 3 Apiaceae 268664 4487456
9 3 Vitaceae 268664 4487458
with these data, I have created ppp for the points of each subplot within a window of general plot (big).
grp <- factor(data$subplot)
win <- ripras(data$X, data$Y)
p.p <- ppp(data$X, data$Y, window = window, marks = grp)
Now I want to divide a plot into equal 3 x 3 sub-plots because there are 9 subplots. The genetal plot is not rectangular looks similar to rombo shape when I plot.
I could use quadrats() funcion as below but it has divided my plot into unequal subplots. Some are quadrat, others are traingle etc which I don't want. I want all the subplots to be equal sized quadrats (divide it by lines that paralel to each sides). Can you anyone guide me for this?
divide <-quadrats(p.patt,3,3)
plot(divide)
Thank you!
Could you break up the plot canvas into 3x3, then run each plot?
> par(mfrow=c(3,3))
> # run code for plot 1
> # run code for plot 2
...
> # run code for plot 9
To return back to one plot on the canvas type
> par(mfrow=c(1,1))
This is a question about the spatstat package.
You can use the function quantess to divide the window into tiles of equal area. If you want the tile boundaries to be vertical lines, and you want 7 tiles, use
B <- quantess(Window(p.patt), "x", 7)
where p.patt is your point pattern.
I am not sure how to make a X-Y plot by R.
I have A B C datasets.
A dataset
ID Result
1.1 2
1.2 4
1.3 2.5
1.4 9
B dataset
ID Result
1.1 1
1.2 7
1.3 6
1.4 9
C dataset
ID Result
1.1 0.5
1.2 8
1.3 9
1.4 9
I want to make a plot X=result A , y=the result B, the other plot x=result A and Y=result C....
then A represented by red spots, B is black and C is blue for example. So the spot 1.1 should be x=2 and y=1 in red (A) and block (B). the spot 4,7, it means it is ID 1.2 in red and block.... The spot 9,9 it means is is ID 1.4 in the red and block.....
I try qqplots but I dont know how to make the X and Y correctly.
Thanks
ggplot2 is an excellent library for producing plots and there are many reference manuals online. Below is an answer to your question using the ggplot approach. The A,B,C data frames are unified into a single frame and the geom_point() for an x-y plot is used. The aes() sets the x and y coordinates (here you seem to seek to plot 'result' as both the x and y, if I understood the question?). The points are scaled by color, which is defined in the data frame as attributes A,B,C. Importantly, this variable must be a factor. The colors are defined by the manual color scale.
library(ggplot2)
dataA <- data.frame(ID=c(1.1,1.2,1.3),result=c(2,4,2.5),index=c(1,2,3),color="A")
dataB <- data.frame(ID=c(1.1,1.2,1.3),result=c(1,7,6),index=c(1,2,3),color="B")
dataC <- data.frame(ID=c(1.1,1.2,1.3),result=c(0.5,8,9),index=c(1,2,3),color="C")
data <- rbind(dataA,dataB,dataC)
data$color <- as.factor(data$color)
ggplot(data) +
geom_point(aes(x=result,y=result,color=color,size=10)) +
scale_color_manual(values=c("red", "black", "blue")) +
theme_bw()
Is it possible to make barplots (two) of unequal size (different max values on Y axis) but equal units (count data)?
The data is count data of the number of nesting attempts per season. Each species has 7 seasons of data. My objective is to present the data as clearly as possible for the reader to show the increase in the number of each of the two species nesting season on season. Although the initial pattern of increase is similar for both species, the number of species 1 nesting rises more rapidly. Plotting both sets of data on the same barplot is not a good option because the 7 seasons of data are not concurrent for the two species - rather it is the first 7 years of colonisation for each species (eg the labels on the x axis are different for the two species)
I have tried par(fig) and layout but not yet achieved what I need and I am not sure which function is better suited to what I need. Any advice welcome
Two barplots, one above the other, each taking up half the window. The Y units are the same for both graphs but the maximum for one is 300 whilst the other is 900. When they are plotted a count of 100 looks very different on the two graphs
SPECIES1 <- c(2,12,44,153,451,857)
SPECIES2 <- c(4,15,35,54,63,243)
windows(11,12)
par(oma=c(3,0.1,1,0.1),mfrow=c(2,1),mar=c(2,6,2,2.1))
barplot(SPECIES2,space=c(0.1,0),ylim=c(0,300),col="black",axes=FALSE)
axis(2,at=seq(0,300,100),las=2, cex.axis=0.9)
barplot(SPECIES1,space=c(0.1,0),ylim=c(0,900), col="black",border=NA,axes=FALSE )axis(2,at=seq(0,900,100),las=2,cex.axis=0.9)
Here how you go by using ggplot package
## supp dose len
## 1 VC D0.5 6.8
## 2 VC D1 15.0
## 3 VC D2 33.0
## 4 OJ D0.5 4.2
## 5 OJ D1 10.0
## 6 OJ D2 29.5
ggplot(data=df2, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity", position=position_dodge())
But you need third variable(supp in above case). Please provide Sample data which you want to plot for clear answer.
Let's say I have data looking like this:
type value
A 1
A 1
A 2
A 2
A 3
B 2
B 2
B 2
B 3
C 2
C 3
C 4
C 5
How can I plot this in one graph, so I have the A, B, and C types on the x-axis, and then the corresponding y-values for each type plotted as dots?
So kind of a scatter plot, but with fixed x-values.
Try using ggplot2. It automatically identifies categorical variables and treats them accordingly.
library(ggplot)
#say your dataframe is stored as data
ggplot(aes(x=data$type,y=data$value))+geom_point()
As Ian points out, this will indeed over plot. You can read about it here. So if you are ok with a 'small amount of random variation to the location of each point', then +geom_jitter is a useful way of handling overplotting.