How to plot a Radar chart in ggplot2 or R [duplicate] - r

This question already has answers here:
creating "radar chart" (a.k.a. star plot; spider plot) using ggplot2 in R
(5 answers)
Closed 7 years ago.
I am trying to make a Radar plot as in attached image using and ggplot2 ( or any other package in R).This talk about this but my case is different as i am trying to create a spider plot for response data with certain range.
I made a plot using a code as below, but i couldn't figure out howto make this like in the image. Kindly help me with this.
Impcts <- c("system","supply","security","well-being")
present <- c(5,5,3,5)
past <- c(6,6,4,5)
group.names <- c("present", "past")
ddf.pre <- data.frame(matrix(c(rep(group.names[1], 4), Impcts), nrow = 4, ncol = 2), var.order = seq(1:4), value = present)
ddf.pas <- data.frame(matrix(c(rep(group.names[2], 4), Impcts), nrow = 4, ncol = 2), var.order = seq(1:4), value = past)
ddf <- rbind(ddf.pre, ddf.pas)
colnames(ddf) <- c("Group", "Impcts", "var.order", "var.value")
library(ggplot2)
ggplot(ddf, aes(y = var.value, x = reorder(Impcts, var.order),
group = Group, colour = Group))+
coord_polar() +
geom_path() +
geom_point()+
labs(title = "Impacts Analysis").

Here is my attempt.First I drew squares using geom_path(). Then, I drew two polygons on top of the squares using geom_polygon(). Finally I added annotations.
### Draw squares
mydf <- data.frame(id = rep(1:6, each = 5),
x = c(0, 6, 0, -6, 0,
0, 5, 0, -5, 0,
0, 4, 0, -4, 0,
0, 3, 0, -3, 0,
0, 2, 0, -2, 0,
0, 1, 0, -1, 0),
y = c(6, 0, -6, 0, 6,
5, 0, -5, 0, 5,
4, 0, -4, 0, 4,
3, 0, -3, 0, 3,
2, 0, -2, 0, 2,
1, 0, -1, 0, 1))
g <- ggplot(data = mydf, aes(x = x, y = y, group = factor(id)) +
geom_path()
### Draw polygons
mydf2 <- data.frame(id = rep(7:8, each = 5),
x = c(0, 6, 0, -5, 0,
0, 5, 0, -5, 0),
y = c(6, 0, -4, 0, 6,
5, 0, -3, 0, 5))
gg <- g +
geom_polygon(data = mydf2, aes(x = x, y = y, group = factor(id), fill = factor(id))) +
scale_fill_manual(name = "Time", values = c("darkolivegreen4", "brown4"),
labels = c("Past", "Present"))
### Add annotation
mydf3 <- data.frame(x = c(0, 6.5, 0, -6.5),
y = c(6.5, 0, -6.5, 0),
label = c("system", "supply", "security", "well-being"))
ggg <- gg +
annotate("text", x = mydf3$x, y = mydf3$y, label = mydf3$label, size = 3)
ggsave(ggg, file = "name.png", width = 10, height = 9)

Related

Sorting barplot based on multi-categories in r

I am trying to get a bar plot for sentiment scores corrected as per the following order and put into two separate colors:
(NEGATIVE) anger, disgust, fear, sadness, negative --- (POSITIVE) anticipation, joy, surprise, trust, positive.
Below is the code which only gives a decreasing plot.
barplot(sort(colSums(s), decreasing = TRUE),
las = 2,
col = rainbow(2),
ylab = 'Count',
main = 'User Synergies')
> dput(head(s))
structure(list(anger = c(1, 0, 0, 0, 0, 0), anticipation = c(0,
0, 5, 0, 0, 0), disgust = c(0, 0, 0, 0, 0, 0), fear = c(1, 0,
2, 1, 0, 0), joy = c(1, 0, 1, 0, 0, 0), sadness = c(1, 0, 2,
1, 0, 0), surprise = c(0, 0, 2, 1, 0, 0), trust = c(4, 2, 3,
1, 0, 1), negative = c(2, 0, 3, 2, 1, 1), positive = c(4, 4,
7, 1, 0, 2)), row.names = c(NA, 6L), class = "data.frame")
Another way:
positive <- c("anticipation", "joy", "surprise", "trust", "positive")
negative <- c("anger", "disgust", "fear", "sadness", "negative")
barplot(colSums(s[,c(negative, positive)]),
las = 2,
col = c(rep("red", length(negative)), rep("cyan", length(positive))),
ylab = 'Count', ylim = c(0, 20),
main = 'User Synergies')
The result:
Try this ,
df <- structure(list(anger = c(1, 0, 0, 0, 0, 0),
anticipation = c(0, 0, 5, 0, 0, 0),
disgust = c(0, 0, 0, 0, 0, 0),
fear = c(1, 0,2, 1, 0, 0),
joy = c(1, 0, 1, 0, 0, 0),
sadness = c(1, 0, 2, 1, 0, 0),
surprise = c(0, 0, 2, 1, 0, 0),
trust = c(4, 2, 3, 1, 0, 1),
negative = c(2, 0, 3, 2, 1, 1),
positive = c(4, 4,7, 1, 0, 2)),
row.names = c(NA, 6L), class = "data.frame")
pn <- rainbow(2) # "#FF0000" "#00FFFF" one for positive and the other for negative
s <- sort(colSums(df) , decreasing = TRUE)
names(s)
#> [1] "positive" "trust" "negative" "anticipation" "fear"
#> [6] "sadness" "surprise" "joy" "anger" "disgust"
# arrange colors based on names of sorted columns
col <- c(pn[1] , pn[1] , pn[2] , pn[1] , pn[2] ,
pn[2] , pn[1] , pn[1] , pn[2] , pn[2])
barplot(s ,
las = 2,
col = col,
ylab = 'Count',
main = 'User Synergies')
Created on 2022-05-31 by the reprex package (v2.0.1)
You may try
library(dplyr)
library(reshape2)
df <- data.frame(
anger = 200,
disgust = 100,
fear = 900,
sadness = 400,
negative = 1500,
anticipation = 2000,
joy = 1200,
surprise = 300,
trust = 2500,
positive = 5000
)
pall <- c("red", "blue")
colSums(df) %>%
melt %>%
tibble::rownames_to_column(., "sentiments") %>%
mutate(sentiments = factor(sentiments, levels = c("anger", "disgust", "fear", "sadness", "negative", "anticipation", "joy", "surprise", "trust", "positive"))) %>%
mutate(colo = ifelse(sentiments %in% c("anger", "disgust", "fear", "sadness", "negative"), 0, 1) %>% as.factor) %>%
barplot(data = ., value ~ sentiments, col = pall[.$colo], las = 2, xlab = "")
Another approach :
df <- structure(list(anger = c(1, 0, 0, 0, 0, 0),
anticipation = c(0, 0, 5, 0, 0, 0),
disgust = c(0, 0, 0, 0, 0, 0),
fear = c(1, 0,2, 1, 0, 0),
joy = c(1, 0, 1, 0, 0, 0),
sadness = c(1, 0, 2, 1, 0, 0),
surprise = c(0, 0, 2, 1, 0, 0),
trust = c(4, 2, 3, 1, 0, 1),
negative = c(2, 0, 3, 2, 1, 1),
positive = c(4, 4,7, 1, 0, 2)),
row.names = c(NA, 6L), class = "data.frame")
s <- sort(colSums(df) , decreasing = TRUE)
pos <- c("positive" , "trust" , "anticipation" ,
"surprise" , "joy")
col <- names(s)
col <- ifelse(col %in% pos , "cyan" , "red")
barplot(s ,
las = 2,
col = col,
ylab = 'Count',
main = 'User Synergies')
Created on 2022-05-31 by the reprex package (v2.0.1)

How to add a frequency or number on scalebar of stacked barplot using geom_text?

I'm a newbie for using Rstudio, so I've some problems I want to ask.
I want to make my scalebar for species composition in 10 sites, and add the number inside the scalebar.
The result like this.
I want to put the frequency number of species composition inside the scale bar. I've been tried to put code of geom_text, but the result is not appropriate at all.
I hope there's an answer to fix this. Thank you so much.
Here is my data, also the coding that I run in R.
data <- as.matrix(data.frame(Bng = c(0, 0, 0, 41, 0, 9, 6, 25, 11, 2, 5, 7),
Krs = c(0, 25, 0, 82, 0, 0, 0, 0, 23, 0, 0, 0),
Bny = c(0, 0, 0, 0, 0, 0, 0, 23, 16, 0, 10, 0),
Kmb = c(1, 0, 0, 0, 20, 0, 0, 25, 8, 1, 0, 0),
Sgk = c(0, 0, 0, 18, 0, 2, 0, 11, 0, 0, 0, 0),
Lwb = c(1, 0, 2, 73, 0, 5, 0, 7, 5, 0, 0, 0),
Lws = c(0, 0, 0, 4, 0, 0, 0, 4, 0, 4, 1, 0),
Krp = c(0, 0, 0, 115, 0, 0, 2, 0, 2, 0, 0, 0),
Hrt = c(4, 0, 0, 0, 2, 22, 0, 7, 4, 2, 3, 0),
Gmb = c(0, 2, 0, 42, 2, 0, 0, 1, 6, 4, 3, 0)))
rownames(data) <- c("Cbr", "Csx", "Rax", "Hdd", "Hlv", "Mst", "Mps", "Mbr", "Rfs", "Rbn", "Rct", "Rps")
data
barplot(data)
barplot(prop.table(data, 2))```
library(reshape2)
data_long <- as.data.frame(data)
data_long$subgroup <- rownames(data_long)
data_long <- melt(data_long, id.vars = "subgroup")
library(ggplot2)
ggp <- ggplot(data_long,
aes(x = variable,
y = value,
fill = subgroup)) +
geom_bar(position = "fill", stat = "identity")+
theme_bw()+
scale_fill_grey()
ggp
ggp +
scale_y_continuous(labels = scales::percent_format())
You may try
library(dplyr)
data_long %>%
group_by(subgroup) %>%
mutate(key = sum(value),
value = value/sum(value)
) %>%
filter(value != 0) %>%
ggplot(aes(x = variable,
y = value,
fill = subgroup)) +
geom_bar(position = "fill", stat = "identity")+
theme_bw()+
scale_fill_grey() +
scale_y_continuous(labels = scales::percent_format()) +
geom_text(aes(label = value * key), position = position_fill(vjust = .5))

R Plotly 3D: Add Mesh to Scatter plot without inheriting color scheme

Basically I have a scatter 3d Scatter plot with the points in two categories (selected, unselected) which are represented in red and grey. To better visualize the selected volume I want to add a cube with low opacity in blue. However, when I add the mesh for the cube, the cube appears in green and the unselected points in orange instead of grey.
In short: Why is the cube not blue and the unselected points not grey and how can I make them do so?
library(shiny)
library(plotly)
ui <- fluidPage(
tags$h2("This is my 3D plot."),
plotlyOutput("Plot3d", width = "1000px", height = "1000px")
)
server <- function(input, output, session){
output$Plot3d <- renderPlotly ({
#Defining data frame for scatter
df_scatter <- data.frame(X_VAL = rnorm(50, mean = 0.5, sd = 0.15),
Y_VAL = rnorm(50, mean = 0.5, sd = 0.15),
Z_VAL = rnorm(50, mean = 0.5, sd = 0.15),
SCATTER_COL = rep("unselected", 50))
#Every point inside of the cube is labeled "selected"
for (i in 1:nrow(df_scatter)){
if (df_scatter$X_VAL[i] < 0.5 && df_scatter$Y_VAL[i] < 0.5 && df_scatter$Z_VAL[i]< 0.5) {
df_scatter$SCATTER_COL[i] <- "selected"
}
}
df_scatter$SCATTER_COL <- factor(df_scatter$SCATTER_COL, levels = c("selected", "unselected"))
#Defining data frame for mesh
df_mesh <- data.frame(X_VAL = c(0, 0, 0.5, 0.5, 0, 0, 0.5, 0.5),
Y_VAL = c(0, 0.5, 0.5, 0, 0, 0.5, 0.5, 0),
Z_VAL = c(0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5),
MESH_COL = factor(rep("CUBE", 8), levels = c("CUBE")))
plot_ly()%>%
add_markers(type = "scatter3d",
mode = "markers",
data = df_scatter,
x = ~X_VAL,
y = ~Y_VAL,
z = ~Z_VAL,
color = ~SCATTER_COL,
colors = c('red', 'grey')) %>%
#Here the trouble starts
add_trace(type = 'mesh3d',
data = df_mesh,
x = ~X_VAL,
y = ~Y_VAL,
z = ~Z_VAL,
i = c(7, 0, 0, 0, 4, 4, 6, 1, 4, 0, 3, 6),
j = c(3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3),
k = c(0, 7, 2, 3, 6, 7, 1, 6, 5, 5, 7, 2),
color = ~MESH_COL,
colors = c("blue"),
inherit = FALSE,
opacity = 0.1
)
})
}
shinyApp(ui = ui, server=server)
Any help is greatly appreciated.
Try facecolor:
library(shiny)
library(plotly)
#> Loading required package: ggplot2
#>
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#>
#> last_plot
#> The following object is masked from 'package:stats':
#>
#> filter
#> The following object is masked from 'package:graphics':
#>
#> layout
mycolors <- colours()[2:10]
ui <- fluidPage(
tags$h2("This is my 3D plot."),
plotlyOutput("Plot3d", width = "1000px", height = "1000px")
)
server <- function(input, output, session){
output$Plot3d <- renderPlotly ({
#Defining data frame for scatter
df_scatter <- data.frame(X_VAL = rnorm(50, mean = 0.5, sd = 0.15),
Y_VAL = rnorm(50, mean = 0.5, sd = 0.15),
Z_VAL = rnorm(50, mean = 0.5, sd = 0.15),
SCATTER_COL = rep("unselected", 50))
#Every point inside of the cube is labeled "selected"
for (i in 1:nrow(df_scatter)){
if (df_scatter$X_VAL[i] < 0.5 && df_scatter$Y_VAL[i] < 0.5 && df_scatter$Z_VAL[i]< 0.5) {
df_scatter$SCATTER_COL[i] <- "selected"
}
}
df_scatter$SCATTER_COL <- factor(df_scatter$SCATTER_COL, levels = c("selected", "unselected"))
#Defining data frame for mesh
df_mesh <- data.frame(X_VAL = c(0, 0, 0.5, 0.5, 0, 0, 0.5, 0.5),
Y_VAL = c(0, 0.5, 0.5, 0, 0, 0.5, 0.5, 0),
Z_VAL = c(0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5))
plot_ly()%>%
add_markers(type = "scatter3d",
mode = "markers",
data = df_scatter,
x = ~X_VAL,
y = ~Y_VAL,
z = ~Z_VAL,
color = ~SCATTER_COL,
colors = c('red', 'grey')) %>%
add_trace(type = 'mesh3d',
data = df_mesh,
x = ~X_VAL,
y = ~Y_VAL,
z = ~Z_VAL,
i = c(7, 0, 0, 0, 4, 4, 6, 1, 4, 0, 3, 6),
j = c(3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3),
k = c(0, 7, 2, 3, 6, 7, 1, 6, 5, 5, 7, 2),
facecolor = rep("blue", 12),
opacity = 0.1
)
})
}
shinyApp(ui = ui, server=server)
Created on 2020-07-02 by the reprex package (v0.3.0)

R - ggplot2 geom_tile creates way different graph to heatmap

I would like to draw a heatmap for some data set.
Originally, I used the heatmap functions with satisfiying results. Yet, as all my other graphs are done with ggplot2, I tried to do it with ggplot2 as well. Yet, I get weird graphs and I am not sure what the reason is.
I am given a dataset as follows:
> dput(B)
structure(list(`2001` = c(510, 15, 14, 9, 8, 11, 7, 5, -1, -3),
`2002` = c(397, -13, 5, 6, 12, -1, 0, 2, 5, 3),
`2003` = c(323, -6, -2, 1, 0, 6, -5, -2, 1, 4),
`2004` = c(133, -2, 2, -4, 0, 5, 8, -2, 0, 1),
`2005` = c(-100, -8, -6, -2, 1, 2, 2, 3, -6, -5),
`2006` = c(-114, -7, 2, -4, -2, 0, 1, 2, 4, -3),
`2007` = c(-130,-13, 0, 4, -3, -2, -1, 1, 2, 4),
`2008` = c(-38, -10, 4, 0, 3, 4, 2, 0, 0, 1),
`2009` = c(-194, -13, -5, -4, -3, -1, 0, 1, 1, 1),
`2010` = c(-202, -6, 0, -1, -5, -2, -3, -1, 2, -2)),
row.names = 0:9, class = "data.frame")
Now using,
> heatmap(as.matrix(B), Colv = NA, Rowv = NA, col = cm.colors(10))
I obtain the following heatmap, which looks reasonable.
On the other hand, with
C <- B
C$size <- row.names(C)
C <- melt(C, variable.name="year",id=c("size"))
ggplot(data = C) +
geom_tile(aes(x = year, y = size, fill = value)) +
scale_fill_gradientn(colors=cm.colors(10))
I get the following
Why is this? And how can I correct this such that I get a plot similar to the first one?
Scaling is applied in heatmap. So you need to scale your data (according to size, in this instance) to reproduce something similar, such as:
C2 <- C %>% group_by(size) %>% mutate(rescale = scale(value))
ggplot(data = C2) +
geom_tile(aes(x = year, y = size, fill = rescale)) +
scale_fill_gradientn(colors=cm.colors(10))
There are other options, depending on what you are trying to visualise. For example, have a look at limits and values in scale_fill_gradientn.
The issue is that size = 0 has much larger numbers than the other entries. So the second heat map is correct because the rest of the values are very close to zero in respect to the given scale. I would say that the second one is actually correct because you can see that outside of size = 0 the numbers are roughly the same, especially in respect to the large numbers found in that first row. I'm not quite sure how heatmap works, and it doesn't supply a scale, but I think the second graph make sense give the low variability outside of the first row.

Decision tree and error matrix calculations

I've created a decision tree using rpart and the code below:
res.tree <- rpart(myformula, data = credit_train)
my data has been subset into 2 parts. The training part at 70% and a testing part at 30%.
This part works well and my tree is created. Where I'm getting stuck is with the prediction so that I can calculate my confusion matrix and ROC curves.
I'm using this code tree_pred = predict(res.tree, credit_train, type = "class")
but I get this message:
Error in predict.rpart(res.tree, credit_test, type = "class") : Invalid prediction for "rpart" object
In addition:
Warning message:
'newdata' had 271 rows but variables found have 729 rows
I can't figure out if I don't have a library loaded or what is causing the it not to recognize the type, which is what so many resources say I need to use and why I'm getting a mismatch in the rows.
The 'newdata' at 271 rows is what my testing data set has and my training data-set has 729 rows.
Is the decision tree creation causing my problem or could it be the prediction code?
Responding to comments:
I'm using the following libraries:
library(readxl)
library(dplyr)
library(factoextra)
library(corrplot)
library(rpart)
library(rpart.plot)
library(RColorBrewer)
library(pROC)
library(Hmisc)
library(fBasics)
library(rattle)
library(caret)
A sample of my data:
structure(list(CHK_ACCT = c(0, 1, 0, 0), DURATION = c(6, 48,
42, 24), HISTORY = c(4, 2, 2, 3), NEW_CAR = c(0, 0, 0, 1), USED_CAR = c(0,
0, 0, 0), FURNITURE = c(0, 0, 1, 0), `RADIO/TV` = c(1, 1, 0,
0), EDUCATION = c(0, 0, 0, 0), RETRAINING = c(0, 0, 0, 0), AMOUNT = c(1169,
5951, 7882, 4870), SAV_ACCT = c(4, 0, 0, 0), EMPLOYMENT = c(4,
2, 3, 2), INSTALL_RATE = c(4, 2, 2, 3), MALE_DIV = c(0, 0, 0,
0), MALE_SINGLE = c(1, 0, 1, 1), MALE_MAR_or_WID = c(0, 0, 0,
0), `CO-APPLICANT` = c(0, 0, 0, 0), GUARANTOR = c(0, 0, 1, 0),
PRESENT_RESIDENT = c(4, 2, 4, 4), REAL_ESTATE = c(1, 1, 0,
0), PROP_UNKN_NONE = c(0, 0, 0, 1), AGE = c(67, 22, 45, 53
), OTHER_INSTALL = c(0, 0, 0, 0), RENT = c(0, 0, 0, 0), OWN_RES = c(1,
1, 0, 0), NUM_CREDITS = c(2, 1, 1, 2), JOB = c(2, 2, 2, 2
), NUM_DEPENDENTS = c(1, 1, 2, 2), TELEPHONE = c(1, 0, 0,
0), FOREIGN = c(0, 0, 0, 0), DEFAULT = c(0, 1, 0, 1), CHK_ACCT_rec = c(1,
2, 1, 1), SAV_ACCT_rec = c(0, 1, 1, 1)), .Names = c("CHK_ACCT",
"DURATION", "HISTORY", "NEW_CAR", "USED_CAR", "FURNITURE", "RADIO/TV",
"EDUCATION", "RETRAINING", "AMOUNT", "SAV_ACCT", "EMPLOYMENT",
"INSTALL_RATE", "MALE_DIV", "MALE_SINGLE", "MALE_MAR_or_WID",
"CO-APPLICANT", "GUARANTOR", "PRESENT_RESIDENT", "REAL_ESTATE",
"PROP_UNKN_NONE", "AGE", "OTHER_INSTALL", "RENT", "OWN_RES",
"NUM_CREDITS", "JOB", "NUM_DEPENDENTS", "TELEPHONE", "FOREIGN",
"DEFAULT", "CHK_ACCT_rec", "SAV_ACCT_rec"), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"))
myformula = credit_train$DEFAULT ~ credit_train$CHK_ACCT_rec +
credit_train$DURATION + credit_train$HISTORY + credit_train$NEW_CAR +
credit_train$USED_CAR + credit_train$FURNITURE + credit_train$`RADIO/TV` +
credit_train$EDUCATION + credit_train$RETRAINING + credit_train$AMOUNT +
credit_train$SAV_ACCT_rec + credit_train$EMPLOYMENT +
credit_train$INSTALL_RATE + credit_train$MALE_DIV + credit_train$MALE_SINGLE
+ credit_train$MALE_MAR_or_WID + credit_train$`CO-APPLICANT` +
credit_train$GUARANTOR + credit_train$PRESENT_RESIDENT +
credit_train$REAL_ESTATE + credit_train$PROP_UNKN_NONE + credit_train$AGE +
credit_train$OTHER_INSTALL + credit_train$RENT + credit_train$OWN_RES +
credit_train$NUM_CREDITS + credit_train$JOB + credit_train$NUM_DEPENDENTS +
credit_train$TELEPHONE + credit_train$FOREIGN
#calimo I hope this is what you needed.

Resources