Re: [R] apply a color range (kind of like a heat map) to the values in each cell of a data frame

2021-06-17 Thread Jim Lemon
Hi Ding,
There are a number of "value to color" functions in various packages.
One is "color.scale" in the plotrix package:

library(plotrix)
s1 <-c(0.085,0.086,0.139,0.129,0.235,0.177,0.000,0.126,0.271,0.000,0.083,0.163)
s2 <-c(0.000,0.093,0.000,0.080,0.072,0.388,0.138,0.107,0.000,0.000,0.474,0.000)
s13 <-c(0.000,0.077,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.166,0.308,0.000)
s3 <-c(0.000,0.478,0.000,0.332,0.163,0.000,0.000,0.000,0.145,0.469,0.000,0.000)
test1 <-data.frame(s1,s2,s13,s3)
# generate a matrix of colors
test1.col<-color.scale(as.matrix(test1),extremes=c("white","blue"))

color.scale calculates colors using linear interpolation of the values
in three color spaces (RGB, HSV or HCL). You may be interested in some
of the plotting functions in plotrix that employ color.scale,
particularly "size_n_color".

Jim

On Fri, Jun 18, 2021 at 10:04 AM Yuan Chun Ding  wrote:
>
> Dear R users,
>
> I have a numeric table with 140 rows and 30 columns, here I only made partial 
> table,  test1,  as an example.  I want to apply a blue color range to the 
> value in each cell of  the data frame test1.
> I found some R code using DT library. However, I only can see the colored 
> table at my R studio viewer window, also only show the first 10 rows.  I hope 
> to save the colored table into a PNG file and want to modify the size of each 
> cell, so I can apply the code to a much bigger table with 140 rows and 30 
> column.
>
> Can you help me?
>
> Thank you very much!!
>
> Ding
>
> s1 
> <-c(0.085,0.086,0.139,0.129,0.235,0.177,0.000,0.126,0.271,0.000,0.083,0.163)
> s2 
> <-c(0.000,0.093,0.000,0.080,0.072,0.388,0.138,0.107,0.000,0.000,0.474,0.000)
> s13 
> <-c(0.000,0.077,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.166,0.308,0.000)
> s3 
> <-c(0.000,0.478,0.000,0.332,0.163,0.000,0.000,0.000,0.145,0.469,0.000,0.000)
> test1 <-data.frame(s1,s2,s13,s3)
> row.names(test1)
> library(DT)
>
> dtable1 <- datatable(test1, rownames=TRUE, options = list(lengthChange = 
> FALSE, dom='t'))
>
> colRamp <- colorRamp(c("white","blue"))
> for(column in names(test1)){
>   x <- na.omit(test1[[column]])
>   brks <- quantile(x, probs = seq(.05, .95, .01))
>   RGB <- colRamp(c(0, (brks-min(x))/(max(x)-min(x
>   clrs <- apply(RGB, 1, function(rgb){
> sprintf("rgb(%s)", toString(round(rgb,0)))
>   })
>   dtable1 <- dtable1 %>%
> formatStyle(column, backgroundColor = styleInterval(brks, clrs))
> }
>
> dtable1
>
> --
> 
> -SECURITY/CONFIDENTIALITY WARNING-
>
> This message and any attachments are intended solely for the individual or 
> entity to which they are addressed. This communication may contain 
> information that is privileged, confidential, or exempt from disclosure under 
> applicable law (e.g., personal health information, research data, financial 
> information). Because this e-mail has been sent without encryption, 
> individuals other than the intended recipient may be able to view the 
> information, forward it to others or tamper with the information without the 
> knowledge or consent of the sender. If you are not the intended recipient, or 
> the employee or person responsible for delivering the message to the intended 
> recipient, any dissemination, distribution or copying of the communication is 
> strictly prohibited. If you received the communication in error, please 
> notify the sender immediately by replying to this message and deleting the 
> message and any accompanying files from your system. If, due to the security 
> risks, you do not wish to r
 eceive further communications via e-mail, please reply to this message and 
inform the sender that you do not wish to receive further e-mail from the 
sender. (LCP301)
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] apply a color range (kind of like a heat map) to the values in each cell of a data frame

2021-06-17 Thread Yuan Chun Ding
Dear R users,

I have a numeric table with 140 rows and 30 columns, here I only made partial 
table,  test1,  as an example.  I want to apply a blue color range to the value 
in each cell of  the data frame test1.
I found some R code using DT library. However, I only can see the colored table 
at my R studio viewer window, also only show the first 10 rows.  I hope to save 
the colored table into a PNG file and want to modify the size of each cell, so 
I can apply the code to a much bigger table with 140 rows and 30 column. 

Can you help me?

Thank you very much!!

Ding

s1 <-c(0.085,0.086,0.139,0.129,0.235,0.177,0.000,0.126,0.271,0.000,0.083,0.163)
s2 <-c(0.000,0.093,0.000,0.080,0.072,0.388,0.138,0.107,0.000,0.000,0.474,0.000)
s13 <-c(0.000,0.077,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.166,0.308,0.000)
s3 <-c(0.000,0.478,0.000,0.332,0.163,0.000,0.000,0.000,0.145,0.469,0.000,0.000)
test1 <-data.frame(s1,s2,s13,s3)
row.names(test1)% 
formatStyle(column, backgroundColor = styleInterval(brks, clrs))
}

dtable1

--

-SECURITY/CONFIDENTIALITY WARNING-  

This message and any attachments are intended solely for the individual or 
entity to which they are addressed. This communication may contain information 
that is privileged, confidential, or exempt from disclosure under applicable 
law (e.g., personal health information, research data, financial information). 
Because this e-mail has been sent without encryption, individuals other than 
the intended recipient may be able to view the information, forward it to 
others or tamper with the information without the knowledge or consent of the 
sender. If you are not the intended recipient, or the employee or person 
responsible for delivering the message to the intended recipient, any 
dissemination, distribution or copying of the communication is strictly 
prohibited. If you received the communication in error, please notify the 
sender immediately by replying to this message and deleting the message and any 
accompanying files from your system. If, due to the security risks, you do not 
wish to receive further communications via e-mail, please reply to this message 
and inform the sender that you do not wish to receive further e-mail from the 
sender. (LCP301)

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R-es] problemas para agrupar cadenas en una sola con str_replace_all

2021-06-17 Thread juan manuel dias
muchas gracias! no encontraba la solución de la forma que me passaste!
lo pude resolver de este modo:

cat_uno <- c("consultorio","consultorio externo","consultorio externos de
cp",
"consultorios cuidados paliativos","consultorios
externos cuidados paliativos",
"consultorios externos de cp","consultorios
externos de cp-",
"consultorios externos de cuidados paliativos","en
hospital: consultorios externos",
"hospital - consultorio
externo","hospital-cons.externo","hospital-cons.externos")

solucion_mult_p8$contexto_atencion <-
sapply(as.vector(solucion_mult_p8$contexto_atencion), function(x) if(x %in%
cat_uno) "Cosultorios Externos" else x )

El mié, 16 jun 2021 a las 20:23, INGESISTEL CIW ()
escribió:

> Podría servirle:
>
>
>
> > categorias <- c("CAMA UCI","CENTRO DE CUIDADOS CRONICOS","CLINICA
> PRIVADA: UTI","CONSULTORIO","CONSULTORIO EXTERNO","CONSULTORIO EXTERNOS DE
> CP","CONSULTORIOS CUIDADOS PALIATIVOS","CONSULTORIOS EXTERNOS CUIDADOS
> PALIATIVOS","CONSULTORIOS EXTERNOS DE CP","CONSULTORIOS EXTERNOS DE
> CP-","CONSULTORIOS EXTERNOS DE CUIDADOS PALIATIVOS","CONTROL TELEFONICO
> POSTALTA","CONVENTO","DISPOSITIVO TELEFONICO","EN CASA/COMUNIDAD","EN
> HOSPITAL: CONSULTORIOS EXTERNOS","EN UN HOGAR DE CUIDADO/HOGAR DE
> ANCIANOS","EN UN HOGAR DE CUIDADO/HOGAR DE ANCIANOS: OTRA SALA","EN UN
> HOGAR DE CUIDADO/HOGAR DE ANCIANOS: SALA PTES. COVID","EN UN HOSPICE O CASA
> DE CUIDADOS PALIATIVOS (NO ESPECIFICAMENTE PARA PACIENTES DE
> CORONAVIRUS)","EN UN HOSPITAL: OTRA SALA","EN UN HOSPITAL: SALA PTES.
> COVID","EN UN HOSPITAL: UTI","GUARDIA SHOCKROOM","HOSPITAL","HOSPITAL -
> CONSULTORIO
> EXTERNO","HOSPITAL-CONS.EXTERNO","HOSPITAL-CONS.EXTERNOS","MEDIANTE VIDEO
> LLAMADAS","NO PROPORCIONE ATENCION AL PACIENTE","SALA DE REANIMACION Y
> SHOCK","SEGUIMIENTO TELEFONICO","TELECONSULTA","TELEFONICAMENTE","VIA
> TELEFONICA","VIEDOLLAMADA-MAILS")
>
>
>
> > CONSULTORIOs_EXTERNOS <- c(categorias[(grepl
> (toupper("consultorio"),categorias))])
>
>
>
> > CONSULTORIOs_EXTERNOS
>
> [1] "CONSULTORIO"
>
>  [2] "CONSULTORIO EXTERNO"
>
>  [3] "CONSULTORIO EXTERNOS DE CP"
>
>  [4] "CONSULTORIOS CUIDADOS PALIATIVOS"
>
>  [5] "CONSULTORIOS EXTERNOS CUIDADOS PALIATIVOS"
>
>  [6] "CONSULTORIOS EXTERNOS DE CP"
>
>  [7] "CONSULTORIOS EXTERNOS DE CP-"
>
>  [8] "CONSULTORIOS EXTERNOS DE CUIDADOS PALIATIVOS"
>
> [9] "EN HOSPITAL: CONSULTORIOS EXTERNOS"
>
> [10] "HOSPITAL - CONSULTORIO EXTERNO"
>
>
>
>
>
> Nota: grepl busca la subcadena deseada
>
>
>
>
>
> Cordial saludo
>
>
>
>
>
> Enviado desde Correo 
> para Windows 10
>
>
>
> *De: *juan manuel dias 
> *Enviado: *miércoles, 16 de junio de 2021 15:27
> *Para: *Lista R 
> *Asunto: *[R-es] problemas para agrupar cadenas en una sola con
> str_replace_all
>
>
>
> Hola, como andan!
>
>
>
> Tengo una base de datos de pacientes y una de las variables es contexto de
> atención en el que fue atendido. El problema es que hay respuestas que
> tiene que ser agrupadas en una misma categoría.
>
>
>
> Por ejemplo, todas estas categorías
>
>
>
> (CONSULTORIO, CONSULTORIO EXTERNO, CONSULTORIO EXTERNOS DE CP,
> CONSULTORIOS CUIDADOS PALIATIVOS,
>
> CONSULTORIOS EXTERNOS CUIDADOS PALIATIVOS, CONSULTORIOS EXTERNOS DE CP,
> CONSULTORIOS EXTERNOS DE CP-,
> CONSULTORIOS EXTERNOS DE CUIDADOS PALIATIVOS,CONSULTORIOS EXTERNOS DE
> CUIDADOS PALIATIVOS,
> CONSULTORIOS EXTERNOS DE CUIDADOS PALIATIVOS)
>
>
>
> Las  debería agrupar en una única "CONSULTORIO EXTERNOS".
>
>
>
> Intenté de esta forma antes habiendo pasado la variable
> "contexto_atencion" toda a mayúsculas y sin acentos.
>
>
>
> pats_consext <- c("CONSULTORIO EXTERNO|CONSULTORIO EXTERNOS DE
> CP|CONSULTORIOS CUIDADOS PALIATIVOS|
> CONSULTORIOS EXTERNOS CUIDADOS PALIATIVOS|CONSULTORIOS EXTERNOS DE
> CP|CONSULTORIOS EXTERNOS DE CP-|
> CONSULTORIOS EXTERNOS DE CUIDADOS PALIATIVOS|CONSULTORIO")
>
> solucion_mult_p8<-solucion_mult_p8 %>%
>   mutate(contexto_atencion = str_replace_all(contexto_atencion,
> pats_consext,'CONSULTORIOS EXTERNOS'))
>
>
>
> Pero no me resuelve el problema.
>
>
>
> Para la categoría
>
>
>
> CONSULTORIOS EXTERNOS CUIDADOS PALIATIVOS
>
>
>
> me trae
>
>
>
> CONSULTORIOS EXTERNOS EXTERNOS CUIDADOS PALIATIVOS
>
>
>
> Alguien sabe cuál puede ser el problema?
>
>
>
> Adjunto la base por las dudas!
>
>
>
> Muchas gracias!
>
>
>
> m
>
>
>
>
>

[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R] Legend position in ggplot

2021-06-17 Thread Felipe Carrillo via R-help
Like this? theme(legend.position= 'top'


On Thursday, June 17, 2021, 10:52:04 AM PDT, peri He 
 wrote:  
 
 Dear Friends,

I would like to see my legend outside of a ggplot (at the top).

This code is showing the legend inside of a plot:
theme(legend.position=c(0.15,0.97))

But when I changed it to : theme(legend.position=c(-0.15,1.5)) , the legend 
disappears.

I would appreciate it if you share your ideas.

Regards,

Peri



    [[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
  
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Legend position in ggplot

2021-06-17 Thread peri He
Dear Friends,

I would like to see my legend outside of a ggplot (at the top).

This code is showing the legend inside of a plot:
theme(legend.position=c(0.15,0.97))

But when I changed it to : theme(legend.position=c(-0.15,1.5)) , the legend 
disappears.

I would appreciate it if you share your ideas.

Regards,

Peri



[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Potential Bug: file.show does not support double-byte characters in the file path

2021-06-17 Thread Tim via R-help
Hi,

I may have come across a bug regarding the file.show function in the base 
package.

### Bug Description

file.show does not support double-byte characters in the file path.

### Platform Info

> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936 LC_CTYPE=Chinese 
(Simplified)_China.936
[3] LC_MONETARY=Chinese (Simplified)_China.936 LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.936

attached base packages:
[1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached):
[1] compiler_4.1.0 tools_4.1.0

### Steps to Reproduce

1. Install R-4.1.0 for Windows on Windows 10 x64 21H1 (Chinese-Simplified, 
zh-CN)
2. Open RGui
3. Create a file named "test.txt" under "C:\Test", so that the full file path 
does not include double-byte characters
4. Create a file named "test.txt" under "C:\测试", so that the full file path 
includes double-byte characters
5. Run the following commands in the console
> t1 = "C:\\Test\\test.txt"
> t1
[1] "C:\\Test\\test.txt"
> file.show(t1)
>
> t2 = "C:\\测试\\test.txt"
> t2
[1] "C:\\测试\\test.txt"
> file.show(t2)
Warning message:
In file.show(t2) : file.show(): file 'C:\测试\test.txt' does not exist

Actual Result:

file.show() can correctly open the "C:\Test\test.txt" as the file path does not 
contain double-byte characters, but it fails to do so for "C:\测试\test.txt".

### Additional information

For users with usernames that include double-byte characters, this bug even 
prevents demo() from functioning properly.

> demo()
Warning message:
In file.show(outFile, delete.file = TRUE, title = paste("R", tolower(x$title))) 
:
file.show():不存在'C:\Users\娴嬭瘯涓瓇1\AppData\Local\Temp\RtmpGuWTun\RpackageIQR197068c75431'这个文件

file.show() can correctly read from files containing double-byte characters if 
the file paths do not. For example, the content for "C:\Test\test.txt" can be 
"Testing 123 测试". file.show("C:\\Test\\test.txt") would open this file and 
display its content correctly without specifying the encoding parameter.

Thanks,
Tim
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Can't add error bars to existing line graph

2021-06-17 Thread Jim Lemon
Hi Bruno,
The interaction.plot function plots the _levels_ of the x.factor
argument, not the values of the variable that is coerced to a factor.
This means that the temperature locations on the x axis are c(1,2),
not c(12,17). The code below works for me:

readscaphfileNEW<-read.table(text="EXPERIMENT TEMP SCAPH.BPM
12021   1282
22021   1258
32021   1278
42021   1259
52021   1280
62021   12   100
72021   1261
82021   12   103
92021   1261
10   2021   17   100
11   2021   1770
12   2021   1783
13   2021   1773
14   2021   17   143
15   2021   17   103
16   2021   1773
17   2021   17   158
18   2021   1795
19   2021   1780
20   1939   12   158
21   1939   12   148
22   1939   12   152
23   1939   12   148
24   1939   12   160
25   1939   12   168
26   1939   12   152
27   1939   12   150
28   1939   12   187
29   1939   17   300
30   1939   17   302
31   1939   17   291
32   1939   17   240
33   1939   17   253
34   1939   17   207
35   1939   17   184
36   1939   17   224
37   1939   17   242
38   1939   17   236",
header=TRUE,stringsAsFactors=FALSE)
interaction.plot(readscaphfileNEW$TEMP,
readscaphfileNEW$EXPERIMENT, readscaphfileNEW$SCAPH.BPM,
xlab = "Temperature (°C)", ylab = "Scaphognathite Rate (BPM)",
main = "Scaphognathite",
ylim = c(0,300), trace.label = "Year",
type = "b", pch = c(19,17), fixed = TRUE)
library(plotrix)
Scaphmeans <-as.vector(by(readscaphfileNEW$SCAPH.BPM,
 readscaphfileNEW[,c("EXPERIMENT","TEMP")],mean))
Scaphse = as.vector(by(readscaphfileNEW$SCAPH.BPM,
 readscaphfileNEW[,c("EXPERIMENT","TEMP")],std.error))
dispersion(c(1,1,2,2),Scaphmeans,Scaphse)

Note that you can get much the same plot using brkdn.plot (plotrix):

brkdn.plot(SCAPH.BPM~EXPERIMENT+TEMP,data=readscaphfileNEW,
 xlab="Temperature C",pch=c(1,2))
legend(15,160,c(1939,2021),pch=c(1,2))

Jim

On Thu, Jun 17, 2021 at 3:38 AM Bruno.Salonen  wrote:
>
>
> Hi all,
>
> I'm trying to add error bars to an existing line graph in base R.
>
> The basic line graph comes up just fine, but it does not show my error
> bars...
>
> Data frame = readscaphfileNEW
> Plot name = SCAPHLINEGRAPHNEW
> x axis = TEMP
> y axis = SCAPH.BPM
> Tracer = Year (SAME AS 'EXPERIMENT)
> Scaphmeans = means of SCAPH.BPM
> Scaphse = standard error of SCAPH.BPM
>
> Here is the code..
>
> SCAPHLINEGRAPHNEW <- interaction.plot(readscaphfileNEW$TEMP,
> readscaphfileNEW$EXPERIMENT, readscaphfileNEW$SCAPH.BPM,
> xlab = "Temperature (°C)", ylab = "Scaphognathite Rate (BPM)",
> main = "Scaphognathite",
> ylim = c(0,300), trace.label = "Year",
> type = "b", pch = c(19,17), fixed = TRUE)
> arrows(SCAPHLINEGRAPHNEW,Scaphmeans+Scaphse,SCAPHLINEGRAPHNEW,Scaphmeans-Scaphse,code=3,
> angle=90, length=0.1)
>
> Why are my error bars not showing? Is the 'arrows' line wrong?
>
> Thanks a million for your help, everybody.
>
> Here is my data set:
>
> > readscaphfileNEW
>EXPERIMENT TEMP SCAPH.BPM
> 12021   1282
> 22021   1258
> 32021   1278
> 42021   1259
> 52021   1280
> 62021   12   100
> 72021   1261
> 82021   12   103
> 92021   1261
> 10   2021   17   100
> 11   2021   1770
> 12   2021   1783
> 13   2021   1773
> 14   2021   17   143
> 15   2021   17   103
> 16   2021   1773
> 17   2021   17   158
> 18   2021   1795
> 19   2021   1780
> 20   1939   12   158
> 21   1939   12   148
> 22   1939   12   152
> 23   1939   12   148
> 24   1939   12   160
> 25   1939   12   168
> 26   1939   12   152
> 27   1939   12   150
> 28   1939   12   187
> 29   1939   17   300
> 30   1939   17   302
> 31   1939   17   291
> 32   1939   17   240
> 33   1939   17   253
> 34   1939   17   207
> 35   1939   17   184
> 36   1939   17   224
> 37   1939   17   242
> 38   1939   17   236
>
> Bruno
>
>
>
> --
> Sent from: https://r.789695.n4.nabble.com/R-help-f789696.html
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see