I'm looking to make a simple bar chart of a random number list (1-10) with the following code
Random.Integer.Data = table(Problem.2.Data$Random.Integer)
barplot(Random.Integer.Data, col = "Orange", main = "Random Integer", xlab = "Number Choice", ylab = "Frequency")
When I use this code, it creates a bar plot with the number 1 6 9 on the x axis. It also only creates 8 bars for me which means it is binning the random integers strangely. I would like their to be a single bar for each number between 1 and 10, and I'd like the x axis to show this as well.
Can someone help me with the code I need in order to do this?
I don't think there's anything wrong with your code. There's something wrong with the assumptions you have about your data.
Let's try to recreate a data frame with the same name and column heading, and we'll fill it with 100 random integers between 1 and 10:
set.seed(69)
Problem.2.Data <- data.frame(Random.Integer = sample(10, 100, TRUE))
head(Problem.2.Data)
#> Random.Integer
#> 1 1
#> 2 2
#> 3 8
#> 4 7
#> 5 7
#> 6 6
Now we'll use your exact code, and see that it works as expected:
Random.Integer.Data = table(Problem.2.Data$Random.Integer)
barplot(Random.Integer.Data,
col = "Orange",
main = "Random Integer",
xlab = "Number Choice",
ylab = "Frequency")
However, let's try it again with a smaller sample size, just 20 rows:
Problem.2.Data <- data.frame(Random.Integer = sample(10, 20, TRUE))
head(Problem.2.Data)
#> Random.Integer
#> 1 8
#> 2 5
#> 3 6
#> 4 8
#> 5 4
#> 6 6
Random.Integer.Data = table(Problem.2.Data$Random.Integer)
barplot(Random.Integer.Data,
col = "Orange",
main = "Random Integer",
xlab = "Number Choice",
ylab = "Frequency")
Now we can see there are only 7 bins. Why? Because if we look at our table, there are only 7 unique integers in our data:
Random.Integer.Data
#>
#> 3 4 5 6 8 9 10
#> 1 1 5 5 5 1 2
We can fix this by making our column a factor with 10 levels:
Random.Integer.Data = table(factor(Problem.2.Data$Random.Integer, 1:10))
barplot(Random.Integer.Data,
col = "Orange",
main = "Random Integer",
xlab = "Number Choice",
ylab = "Frequency")
Also note that if I shrink the window down some of the x axis labels disappear to prevent overlaps:
Created on 2020-08-16 by the reprex package (v0.3.0)
If you want 10 bins and you feel like using something else then ggplot2 can give you the same result. As mentioned above, you also need that column with 10 factors.
#install.packages("ggplot2")
library(ggplot2)
df<-data.frame(x=seq(1:10),
y=sample(10,10,TRUE)) #your data
ggplot(df,aes(x=factor(x),y=y))+
geom_col(fill="Orange")+
labs(title = "Random Integer", x = "Number Choice", y = "Frequency")
Related
I am working with R. Suppose I have the following data frame:
my_data <- data.frame(
"col" = c("red","red","red","red","red","blue","blue","blue","blue","blue","green", "green", "green", "green","green"),
"x_cor" = c(1,2,5,6,7,4,9,1,0,1,4,4,7,8,2),
"y_cor" = c(2,3,4,5,9,5,8,1,3,9,11,5,7,9,1),
"frame_number" = c(1,2,3,4,5, 1,2,3,4,5, 1,2,3,4,5)
)
my_data$col = as.factor(my_data$col)
head(my_data)
col x_cor y_cor frame_number
1 red 1 2 1
2 red 2 3 2
3 red 5 4 3
4 red 6 5 4
5 red 7 9 5
6 blue 4 5 1
In R, is it possible to create a (two-dimensional) graph that will "animate" each colored point to a new position based on the "frame number"?
For example:
I started following the instructions from this website here: https://www.datanovia.com/en/blog/gganimate-how-to-create-plots-with-beautiful-animation-in-r/
First, I made a static graph:
library(ggplot2)
library(gganimate)
p <- ggplot(
my_data,
aes(x = x_cor, y=y_cor, colour = col)
Then, I tried to animate it:
p + transition_time(frame_number) +
labs(title = "frame_number: {frame_number}")
Unfortunately, this produced an empty plot and the following warnings:
There were 50 or more warnings (use warnings() to see the first 50)
1: Cannot get dimensions of plot table. Plot region might not be fixed
2: values must be length 1,
but FUN(X[[1]]) result is length 15
Can someone please show me how to fix this problem?
Thanks
I have gotten a great graph by two distinct electrodes with the following code:
elec <- c("Fp1","Fp2","F7","F3","Fz","F4","F8","FC5","FC1","FC2","FC6","C3","Cz","C4","CP5","CP1","CP2","CP6","P7","P3","Pz","P4","P8","POz","Oz",
"Fp1","Fp2","F7","F3","Fz","F4","F8","FC5","FC1","FC2","FC6","C3","Cz","C4","CP5","CP1","CP2","CP6","P7","P3","Pz","P4","P8","POz","Oz")
edgelist <- get.edgelist(net)
# get vertex labels
label <- get.vertex.attribute(net, "name")
# get vertex groups
group <- get.vertex.attribute(net, "group")
# get vertex fill color
fill <- get.vertex.attribute(net, "color")
# get family
family <- get.vertex.attribute(net, "family")
# get vertex degree
degrees <- degree(net)
# data frame with groups, degree, labels and id
nodes <- data.frame(group, degrees, family, label, fill, id=1:vcount(net))
nodes$family <- factor(nodes$family, levels = unique(nodes$family))
nodes$label <- factor(nodes$label, levels = unique(nodes$label))
nodes <- as_tibble(nodes)
# prepare data for edges
edges <- as_tibble(edgelist)
net.tidy <- tbl_graph(nodes = nodes, edges = edges, directed = TRUE, node_key = "label")
ggraph(net.tidy, layout = "linear") +
geom_edge_arc(alpha = 0.5) +
scale_edge_width(range = c(0.2, 2)) +
scale_colour_manual(values= vrtxc) +
geom_node_point(aes(size = degrees, color = family)) +
geom_node_text(aes(label = elec), angle = 90, hjust = 1, nudge_y = -0.5, size = 3) +
coord_cartesian(clip = "off") +
theme_graph()+
theme(legend.position = "top")
And I got a great graph that I like.
However, I would like to separate both sets of electrodes, in the middle, where Oz is, a little bit, to see there is a difference. In the node attributes I have them differentiated by group (1,2), and I would like to know whether this information could be used to expand through the x axis both sets of vertices, on set of 25 electrodes to the left, the other one to the right of the axis, leaving a space in the middle of them.
I attach some look at the data in case its useful
> nodes
# A tibble: 50 x 6
group degrees family label fill id
<fct> <dbl> <fct> <fct> <fct> <int>
1 1 5 fronp Fp1_1 #3B9AB2 1
2 1 9 fronp Fp2_1 #3B9AB2 2
3 1 6 fron F7_1 #5DAABC 3
4 1 7 fron F3_1 #5DAABC 4
5 1 11 fron Fz_1 #5DAABC 5
6 1 9 fron F4_1 #5DAABC 6
7 1 11 fron F8_1 #5DAABC 7
8 1 8 fronc FC5_1 #88BAAE 8
9 1 6 fronc FC1_1 #88BAAE 9
10 1 4 fronc FC2_1 #88BAAE 10
# … with 40 more rows
In case someone is interested, I ended up inserting some dummy rows in the nodes dataframe and it did the trick.
I've run a PCA with a moderately-sized data set, but I only want to visualize a certain amount of points from that analysis because they are from repeat observations and I want to see how close the paired observations are to each other on the plot. I've set it up so that the first 18 individuals are the ones I want to plot, but I can't seem to only plot just the first 18 points without only doing an analysis of only the first 18 instead of the whole data set (43 individuals).
# My data file
TrialsMR<-read.csv("NER_Trials_Matrix_Retrials.csv", row.names = 1)
# I ran the PCA of all of my values (without the categorical variable in col 8)
R.pca <- PCA(TrialsMR[,-8], graph = FALSE)
# When I try to plot only the first 18 individuals with this method, I get an error
fviz_pca_ind(R.pca[1:18,],
labelsize = 4,
pointsize = 1,
col.ind = TrialsMR$Bands,
palette = c("red", "blue", "black", "cyan", "magenta", "yellow", "gray", "green3", "pink" ))
# This is the error
Error in R.pca[1:18, ] : incorrect number of dimensions
The 18 individuals are each paired up, so only using 9 colours shouldn't cause an error (I hope).
Could anyone help me plot just the first 18 points from a PCA of my whole data set?
My data frame looks similar to this in structure
TrialsMR
Trees Bushes Shrubs Bands
JOHN1 1 4 18 BLUE
JOHN2 2 6 25 BLUE
CARL1 1 3 12 GREEN
CARL2 2 4 15 GREEN
GREG1 1 1 15 RED
GREG2 3 11 26 RED
MIKE1 1 7 19 PINK
MIKE2 1 1 25 PINK
where each band corresponds to a specific individual that has been tested twice.
You are using the wrong argument to specify individuals. Use select.ind to choose the individuals required, for eg.:
data(iris) # test data
If you want to rename your rows according to a specific grouping criteria for readily identifiable in a plot. For eg. let setosa lies in series starting with 1, something like in 100-199, similarly versicolor in 200-299 and virginica in 300-399. Do it before the PCA.
new_series <- c(101:150, 201:250, 301:350) # there are 50 of each
rownames(iris) <- new_series
R.pca <- prcomp(iris[,1:4],scale. = T) # pca
library(factoextra)
fviz_pca_ind(X= R.pca, labelsize = 4, pointsize = 1,
select.ind= list(name = new_series[1:120]), # 120 out of 150 selected
col.ind = iris$Species ,
palette = c("blue", "red", "green" ))
Always refer to R documentation first before using a new function.
R documentation: fviz_pca {factoextra}
X
an object of class PCA [FactoMineR]; prcomp and princomp [stats]; dudi and pca [ade4]; expOutput/epPCA [ExPosition].
select.ind, select.var
a selection of individuals/variables to be drawn. Allowed values are NULL or a list containing the arguments name, cos2 or contrib
For your particular dummy data, this should do:
R.pca <- prcomp(TrailsMR[,1:3], scale. = TRUE)
fviz_pca_ind(X= R.pca,
select.ind= list(name = row.names(TrialsMR)[1:4]), # 4 out of 8
pointsize = 1, labelsize = 4,
col.ind = TrialsMR$Bands,
palette = c("blue", "green" )) + ylim(-1,1)
Dummy Data:
TrialsMR <- read.table( text = "Trees Bushes Shrubs Bands
JOHN1 1 4 18 BLUE
JOHN2 2 6 25 BLUE
CARL1 1 3 12 GREEN
CARL2 2 4 15 GREEN
GREG1 1 1 15 RED
GREG2 3 11 26 RED
MIKE1 1 7 19 PINK
MIKE2 1 1 25 PINK", header = TRUE)
I have the following problem:
I need to create a mosaic plot but want to display the number of cases for each mosaic, as total numbers per country differ. The plot is based on the following data:
1 - not agree 2 3 4 5 - fully agree
DE 6 2 0 0 1
ES 5 3 1 1 0
FR 6 3 1 2 0
SE 4 3 0 0 0
I used the following code:
> mosaicplot(Q1, col=c("red", "orange", "yellow", "green", "green4"),
+ las = 1,
+ main = "There is no need to do anything about it.",
+ ylab = "",
+ xlab = "Country")
Giving me this graph:
Now I would like to divide the first red bar into six bars of the same colour, as there were 6 votes in Germany a.s.o. Any ideas on how to accomplish that?
I applied the procedure explained here:
https://learnr.wordpress.com/2009/03/29/ggplot2_marimekko_mosaic_chart/
Only I had to use two data frames, one for the percentages and one for the absolute values.
Both data frames went through the same calculations. Whilst dfm1 created the chart, dfm21 was used for the labels:
p2 <- p1 + geom_text(aes(x = xtext, y = ytext,
label = ifelse(dfm21$value == "0", paste(" "), paste(dfm21$value))), size = 3.5)
I have some data as following:
xlab ylab xval yval class
1 2 6015 10500 3
1 7 6015 9696 1
3 5 6632 15626 3
3 6 6632 6074 1
3 5 6632 4189 2
1 3 6015 6632 1
1 5 6015 15626 1
I want to create a scatter plot using with x and y coordinate specified in xval and yval columns. Each point is colored based on the value in class column. This part I could do as following:
<!-- language: lang-R -->
data <- read.table(filename, header=TRUE);
df <- data.frame(data["xval"], data["yval"], data["class"]);
plot(xval, yval, col=c("red", "blue", "green")[class],
main="Title here",
xlab="Ox title",
ylab="Oy title");
Now, I want to have a label for each node using the xlab and ylab columns in the form of (xlab, ylab). Eg. The first node is labeled as (1,2). However, I couldn't figured out how to do it.
Can someone please show me how to add labels to nodes pleases? I'm a newbie in R so I'd really appreciate if you could show me the sample code.
Many thanks,
Use the text function as follows:
with(df, plot(xval, yval, col=c("red", "blue", "green")[class],
main="Title here",
xlab="Ox title",
ylab="Oy title"))
with(df, text(xval, yval, sprintf("(%s, %s)", xlab, ylab)))
I use sprint here but you could use paste/paste0 as well.