Sort table in descending order - r

Based on the data and code below, is it possible to sort the table in descending order?
Data (df):
structure(list(CITYNAME = c("a", "b", "c",
"d", "e", "f", "g",
"h", "i", "j", "k",
"l", "m", "n", "p", "q",
"r", "s", "t", "u",
"w", "x", "y", "z"), AvgPpt = c(127.785,
131.456, 128.357, 114.792, 131.383, 129.696, 137.008, 136.129,
132.881, 131.676, 129.103, 132.475, 122.263, 132.393, 134.552,
120.322, 125.987, 132.337, 131.18, 122.705, 123.285, 128.853,
134.494, 114.154)), row.names = c(NA, -24L), class = c("tbl_df",
"tbl", "data.frame"))
Code:
library(ggpubr)
tbl_ppt = df %>%
ggtexttable(cols = c("Municipilality", "Average Precipitaiton (mm)"),
rows = NULL,
theme = ttheme("mBlue"))
tbl_ppt

You could arrange your data in your desired order before passing it to ggtexttable:
library(ggpubr)
library(dplyr)
df %>%
arrange(desc(AvgPpt)) %>%
ggtexttable(cols = c("Municipilality", "Average Precipitaiton (mm)"),
rows = NULL,
theme = ttheme("mBlue"))

Related

echarts4r error at plotting barchart's colors

As you can see in the annexed image it seems that there is a problem with the e_visual_map() function from echarts4r. After y = aa the colors stop changing. Is there a way to fix the bar's colors?
data
library(dplyr)
library(viridis)
library(echarts4r)
df <- structure(
list(
Market = c("a", "b", "c", "d", "e",
"f", "g", "h", "i", "j",
"k", "l", "m", "n", "o",
"p", "q", "r", "s", "t",
"u", "v", "w", "x", "y",
"z", "aa", "bb", "cc",
"dd", "ee", "ff", "gg",
"hh", "ii", "jj", "kk",
"ll", "mm", "nn", "oo",
"pp", "qq", "rr", "ss",
"tt", "uu", "vv"),
Percent_Change = c(5.16901350940851, 3.91868856906443, 3.41802504497987, 3.16413673886071,
3.12684219659363, 2.89249688621206, 2.87284606849977, 2.84454222482254,
2.57058275282915, 2.43282934768581, 2.34818492965906, 2.30880001810456,
2.2918613260413, 2.24101659933832, 2.18752627680741, 2.10073586714032,
1.86045092759311, 1.85290305266011, 1.68128474330245, 1.54700002004653,
1.5303536712395, 1.52152376952798, 1.45917880532612, 1.4355692973819,
1.4257368870892, 1.36409659669896, 1.22315092771929, 1.04309133074753,
0.939025651002292, 0.844389462321624, 0.797407599768931, 0.681691408815433,
0.242176237950194, 0.237798995363376, 0.219182593926239, -0.0280421490193321,
-0.111286439923117, -0.124395342178022, -0.175922623382462, -0.188080671185304,
-0.870155958402443, -1.60611679230328, -1.66206110148814, -1.82732601610943,
-3.68051100830324, -4.43292411223474, -9.42691532047856, -10.5405968097707)),
row.names = c(NA, -48L), class = c("tbl_df", "tbl", "data.frame"))
code to plot
df %>% arrange(Percent_Change) %>%
# mutate(Market = fct_reorder(Market, -Percent_Change)) %>%
e_chart(Market) %>% e_bar(Percent_Change) %>%
e_visual_map(Percent_Change, scale = e_scale, color = viridis(100)) %>%
e_flip_coords() %>%
e_legend(show = F) %>%
e_color(background = c("#343E48"))
Results
Not 100% about your desired result. If you want to color your bars according to the value of PercentChange than this could be achieved by setting dimension=0 to color by the x axis and getting rid of scale:
library(dplyr)
library(viridis)
library(echarts4r)
df %>%
arrange(Percent_Change) %>%
e_chart(Market) %>%
e_bar(Percent_Change) %>%
e_visual_map(Percent_Change,
color = viridis(10),
dimension = 0
) %>%
e_flip_coords() %>%
e_legend(show = FALSE) %>%
e_color(background = c("#343E48"))

Error for graph saving loop - Must be length 1 (a summary value)

I'm trying to create and save graphs for individual organizations. I keep getting an error that says "Error in summarise_impl(.data, dots) :
Column Improved must be length 1 (a summary value), not 0"
The graphs work when I combine all the organizations together, so I'm not sure what is going on here!
Starting with this data:
library(ggpubr)
structure(list(Organization = c("A", "B", "C", "D", "E", "F",
"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S"
), imp_imp20_Improved = c(55.6, 100, 50, 0, 57.1, 0, 0, 45, 50,
60, 100, 50, 66.7, 66.7, 33.3, 0, 50, 0, 50)), row.names = c(NA,
-19L), class = c("tbl_df", "tbl", "data.frame"))
org<- c("A", "B", "C", "D", "E", "F",
"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S"
)
This is my code for the graph loop:
for(i in org) {
tiff(paste0("//graphs/",i,"_graph11.tiff"), units="in", width=3.5, height=3, res=300)
indicator_graph1<- indicators_ong %>%
filter(Organization==i) %>%
summarise(Improved = imp_imp20_Improved,
"Not Improved" = 100-imp_imp20_Improved)%>%
gather(key="group") %>%
arrange(desc(group))
labs <- paste0(indicator_graph1$group, "\n (", indicator_graph1$value,"%)")
z <- ggpie(indicator_graph1,"value",label=labs, fill= "group", color = "black", palette = c("darkgoldenrod1","azure3"), lab.pos = "in", lab.font = c(3,"black"),title="Improve 20")+
theme(legend.position ="none")+
font("title", size=10, hjust=0.5)
print(z)
dev.off()
}

Distance matrix from proxy package into a dataframe

I have a distance matrix from this code
I would like to convert the distanceMatrix into a dataframe. I use this:
library(reshape2)
melt(distanceMatrix)
or
as.data.frame(distanceMatrix)
and I receive this error:
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ""crossdist"" to a data.frame
Data
distanceMatrix <-
structure(c(1.1025096478618, 2.48701192612548, 1.81748937453859,
0.68928345814907, 3.4194165172611, 1.39021901561926, 0.696405607391678,
1.09511501308162, 0.733071057157832, 0.894074317336616, 0.274302486490285,
2.00790247099612, 2.03702210657379, 0.790303515570192, 0.76573433957666,
1.0571870370502, 2.08607605440225, 1.18691928628668, 0.950127106192438,
1.90183580897689, 1.06791623757733, 1.95426617861089, 1.28359907050968,
0.639828869115434, 1.2125883228325, 1.17334881171837, 2.86424081724093,
4.29579721901031, 2.48106485650871, 2.47992202769688, 4.78094585963798,
3.08269692108197, 2.51054397059837, 2.78351950724781, 1.9552995309483,
1.02672164296738, 2.04833064878561, 2.40777909325915, 1.37714830319657,
2.54290296394426, 1.99486295133513, 1.42661425293529, 2.75973709232752,
0.632464187558431, 2.64349038129557, 3.04900615202494, 1.34349249286485,
0.66548291586285, 1.14201671902258, 2.20314775706901, 3.027560891124,
2.58016468923376, 0.701837450761437, 1.82650318310107, 1.17318969224049,
0.898229996978744, 2.04804918964036, 0.510384590416117, 1.20067408397491,
0.479351971313752, 0.900264653292786, 2.17660319096498, 1.11774249289539,
1.50312712068438, 2.35380779446751, 0.74568873241509, 0.860144296532242,
1.49609968893816, 1.27903173482324, 2.30242237929782, 0.546178045451667,
0.696804454166844, 1.57330737370915, 3.18912158434627, 2.63481498585198,
0.743304574607114, 1.2813138290548, 0.278296684614969), .Dim = c(26L,
3L), .Dimnames = list(c("a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z"), c("A", "B", "C")), class = "crossdist", method = "Euclidean", call = proxy::dist(x = voterIdealPoints,
y = candidateIdealPoints))
Use
as.dataframe(as.matrix(distanceMatrix))

How to order data.frame in my specific 'vector' order in R language?

I have a data.frame showed below:
In order to analyse the relationship between those 10 features and disorder propensity, I need to sort the data.frame in my amino acids order which is stored in an vector like this c("L", "I", "V", "Y", "C", "F", "R", "W", "M", "H", "N", "T", "G", "D", "Q", "A", "K", "S", "P", "E")
I tried this properties[aa == c("L", "I", "V", "Y", "C", "F", "R", "W", "M", "H", "N", "T", "G", "D", "Q", "A", "K", "S", "P", "E"), ] which doesn't seem to work for me.
What's the right way to sort the data.frame in my 'vector' order?
You can make your column aa a factor and give the factor levels in the correct order. The factor can then be sorted according to the levels. Look at this example:
my_order <- c("X", "Y", "Z", "A", "B") # defines the order
test <- c("A", "B", "Y", "Z", "Z", "A", "X", "X", "B") # a normal character vector
test2 <- factor(test, levels = my_order) # convert it to factor and specify the levels
test2 # original order unchanged
test2[order(test2)] # ordered by custom order
Note that you must specify all occuring factor levels or this will not work!

Replacement with vectors

I have a vector with all consonants and I want every single consonant to be replaced with a "C" in a given data frame. Assume my data frame is x below:
x <- c("abacate", "papel", "importante")
v <- c("a", "e", "i", "o", "u")
c <- c("b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z")
find <- c
replace <- "C"
found <- match(x, find)
ifelse(is.na(found), x, replace[found])
This is not working. Could anybody tell me what the problem is and how I can fix it?
Thanks
Regular expressions (gsub) are far more flexible in general, but for that particular problem you can also use the chartr function which will run faster:
old <- c("b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n",
"p", "q", "r", "s", "t", "v", "w", "x", "y", "z")
new <- rep("C", length(old))
chartr(paste(old, collapse = ""),
paste(new, collapse = ""), x)
Use gsub to replace the letters in a character vector:
c <- c("b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z")
consonants = paste(c("[", c, "]"), collapse="")
replaced = gsub(consonants, "C", x)
consonants becomes a regular expression, [bcdfghjklmnpqrstvwxyz], that means "any letter inside the brackets."
One of the reasons your code wasn't working is that match doesn't look for strings within other strings, it only looks for exact matches. For example:
> match(c("a", "b"), "a")
[1] 1 NA
> match(c("a", "b"), "apple")
[1] NA NA

Resources