How to pass variables to 'text' in racket - text

I have to create the above image
The text inside is variable. What I mean is that I want to pass 2 values to it for display and these values keep changing.
I am trying this:
(define (circle-text vx vy)
(underlay/align "center"
"center"
(circle 40 "outline" "blue")
(text "("vx", "vy")" 14 "blue")))
But this obviously doesnt work. Please suggest me any better syntax or anything.

Try this:
(text (string-append "(" vx ", " vy ")") 14 "blue")
The above works assuming that vx and vy are already strings. If not, use this:
(text (string-append "(" (number->string vx) ", " (number->string vy) ")") 14 "blue")
Given that you're using Racket, this is even simpler:
(text (format "(~a, ~a)" vx vy) 14 "blue")

Related

In drc() package, drm fct = L.4 finds wrong intercept parameters, even though the graph is right

I have a problem with the following code.
It calculates the drc curve correctly, but the ec50 wrongly, although the are closely related...
x <- c(-1, -0.114074, 0.187521, 0.363612, 0.488551, 0.585461, 0.664642, 0.730782, 0.788875, 0.840106, 0.885926, 0.92737, 0.965202, 1)
y <- c(100, 3.978395643, 0.851717911, 0.697307565, 0.512455497, 0.512455497, 0.482273052, 0.479293487, 0.361024717, 0.355324864, 0.303120838, 0.286539832, 0.465692047, 0.358045152)
mat <- cbind(x, y)
df <- as.data.frame(mat)
calc <- drm(
formula = y ~ x,
data = df,
fct = L.4(names = c("hill", "min_value", "max_value", "ec50"))
)
plot <- ggplot(df, aes(x=x, y=y), color="black") +
geom_point() +
labs(x = "x", y = "y") +
theme(
axis.title.x = element_text(color="black", size=10),
axis.title.y = element_text(color="black", size=10),
axis.line.x = element_line(color = "black"),
axis.line.y = element_line(color = "black")
) +
stat_smooth(
formula = y ~ x,
method = "drm", color="black",
method.args = list(fct = L.4(names = c("hill", "min_value", "max_value", "ec50"))),
se = FALSE
) +
theme(panel.background=element_rect(fill="white"))+
ylim(0, NA)
ec50 <- ED(calc,50)
print(ec50)
print(calc)
print(plot)
This is the graph I obtain:
But if I print the parameters of the function L.4, I have the following result:
hill:(Intercept) 6.3181
min_value:(Intercept) 0.3943
max_value:(Intercept) 111.0511
ec50:(Intercept) -0.6520
max_value:(Intercept) is obviously wrong (it has to be 100), and, as a consequence, ec50 is wrong too.
I would also add that for other sets of data, the min_value:(Intercept) is wrong too (with values < 0...)
I cannot find the mistake, because the graph derived from the same function L.4 shows the right values.
Thank you very much for your help!
The upper asymptote in your case assumes a symmetrical curve (due to 4PL fitting). Meaning that both bottom and upper asymptote have the same inflection.
Your data might max out at 100 but the formula calculates the upper asymptote further than 100 (111) because that's where the actual asymptote lies, not the end of your data.
So the graph is based on your data, but the estimated parameters forces a symmetrical curve to fit it, and your asymptote increases. This will also shift the EC50.

How to include field names when printing a Racket struct

Suppose I have this Racket code:
(struct pos (x y))
(displayln (pos 5 6))
This displays #<pos>. Is there a way to make it display the field names and values too?
With the #:transparent option, the values are displayed:
(struct pos (x y) #:transparent)
(displayln (pos 5 6))
This displays #(struct:pos 5 6), but I also want to display the field names (x and y). Is there a way to display both the field names and values? For example: #(struct:pos #:x 5 #:y 6).
I am looking for something similar to how Common Lisp structs are displayed. Common Lisp example:
(defstruct pos x y)
(format t "~A~%" (make-pos :x 5 :y 6))
This prints #S(POS :X 5 :Y 6).
If you don't want to use third-party libraries, take a look at the very last example of make-constructor-style-printer.
If you don't mind using third-party libraries, you can just use Rebellion's record.

Coloring ALV row where field value > n

How can I display the row where the sum of ls_out is 1900? How can I improve below code?
TABLES: mara, marc.
"marc is N 181
"mara is 1 157
DATA: lt_mara TYPE TABLE OF mara,
ls_mara TYPE mara,
lt_marc TYPE TABLE OF marc,
ls_marc TYPE marc,
BEGIN OF ls_out OCCURS 0,
mtart LIKE mara-mtart,
matnr LIKE marc-matnr,
werks LIKE marc-werks,
ntgew LIKE mara-ntgew,
brgew LIKE mara-brgew,
sum LIKE mara-brgew,
color(4).
DATA: END OF ls_out.
DATA: lt_out LIKE TABLE OF ls_out,
fcat TYPE slis_t_fieldcat_alv,
ls_fcat LIKE LINE OF fcat,
layout TYPE slis_layout_alv.
FIELD-SYMBOLS: <fsym> LIKE LINE OF fcat.
PARAMETERS: p_mtart TYPE mara-mtart. "FERT
SELECT-OPTIONS: so_werks FOR marc-werks. " 1000 to 1998
SELECT * FROM mara INTO TABLE lt_mara
WHERE mtart = p_mtart.
IF sy-subrc = 0.
SELECT * FROM marc INTO TABLE lt_marc
FOR ALL ENTRIES IN lt_mara
WHERE matnr = lt_mara-matnr
AND werks IN so_werks.
LOOP AT lt_marc INTO ls_marc.
READ TABLE lt_mara INTO ls_mara
WITH KEY matnr = ls_marc-matnr.
ls_out-sum = ls_mara-brgew + ls_mara-ntgew .
MOVE-CORRESPONDING ls_marc TO ls_out.
MOVE-CORRESPONDING ls_mara TO ls_out.
APPEND ls_out TO lt_out.
CLEAR ls_out.
ENDLOOP.
ELSE.
MESSAGE TEXT-e02 TYPE 'E' .
ENDIF.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = sy-repid "e merr auto
i_internal_tabname = 'LS_OUT'
i_client_never_display = 'X'
i_inclname = sy-repid
CHANGING
ct_fieldcat = fcat[]
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
READ TABLE fcat INDEX 6 ASSIGNING <fsym>.
<fsym>-outputlen = 15.
*-conditionally populate the color
LOOP AT LS_OUT.
IF LS_OUT-sum eq 21.
LS_OUT-color = 'C311'.
ENDIF.
MODIFY LS_OUT.
ENDLOOP.
layout-info_fieldname = 'COLOR'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_structure_name = 'LS_OUT'
it_fieldcat = fcat[]
TABLES
t_outtab = lt_out
EXCEPTIONS
program_error = 1
OTHERS = 2.
Adding the following parameter to your call to REUSE_ALV_GRID_DISPLAY should fix your problem
is_layout = layout
If you want to handle ANY /!\ formatting of a particular cell in ALV, here s the way.
FORM update_style using
x type lvc_fname " x is 'MATNR', 'VBELN', 'BELNR'.... whatever in your fieldcatalog
y type int4 " y = 1,2,3.....
update_mask type xstring
update_style type xstring.
READ table gt_alv into gs_alv index y.
tabstyle = gs_alv-tabstyle.
READ table tabstyle into s_style with key fieldname = x.
" check if fieldname already exists in gs_alv-tabstyle (insert or update)
s_style-style = s_style-style BIT-AND update_mask.
s_style-style = s_style-style BIT-OR update_style.
" Update or insert gs_alv-tabstyle from s_style
" update gt_alv from gs_alv
ENDFORM.
How to assign a color to a row in ALV
(But you cant have Cell background and Font Color at the same time !)
perform update_style USING x y 'FFFFFFE0' update_style.
update_style can be :
'00000000'. " Disable any color
'00000011'. " White Font
'00000017'. " Red fond
'00000013'. " gray font
'00000014'. " Yellow font
'00000015'. " blue font
'00000016'. " green font
'00000018'. " orange font
'00000011'. " black font
'0000000D'. " background dark blue
'0000000A'. " background fluo blue
'00000005'. " background blue
'00000003'. " background light blue
'00000000'. " background gray
'00000001'. " background light gray
'00000004'. " backgorund yellow
'0000000C'. " background dark yellow
'00000006'. " background green
'0000000E'. " background dark green
'00000010'. " background dark orange
'0000000F'. " background red
'00000007'. " background pink
'00000008'. " background orange
'00000001'. " background standard
If you want to handle editable, bold, italic, underline... properties
perform update_style USING x y 'FFFFFF9F' '00000020'. " set bold
perform update_style USING x y 'FFFFFF9F' '00000040'. " unset bold property
perform update_style USING x y 'FFFFF9FF' '00000200'. " Underline
perform update_style USING x y 'FFFFF9FF' '00000400'. " stop underlying
perform update_style USING x y 'FFFFF57F' '00000080' " italic
perform update_style USING x y 'FF9FFFFF' '00200000' " hotspot
perform update_style USING x y 'FF9FFFFF' '00400000' " no hotspot
perform update_style USING x y 'F9E7FFFF' '00080000' " Editable
perform update_style USING x y 'F9E7FFFF' '00100000' " Non Editable
For alignments, update_mask is 'DFFFF57F'
And update_style can be :
DATA alv_style_align_left_top(4) TYPE x VALUE '00000800'.
DATA alv_style_align_center_top(4) TYPE x VALUE '00001000'.
DATA alv_style_align_right_top(4) TYPE x VALUE '00001800'.
DATA alv_style_align_left_center(4) TYPE x VALUE '00002000'.
DATA alv_style_align_center_center(4) TYPE x VALUE '00002800'.
DATA alv_style_align_right_center(4) TYPE x VALUE '00003000'.
DATA alv_style_align_left_bottom(4) TYPE x VALUE '00003800'.
DATA alv_style_align_center_bottom(4) TYPE x VALUE '00004000'.
DATA alv_style_align_right_bottom(4) TYPE x VALUE '00004800'.
If you want to handle borders, you have to modify s_style-style2 with :
'FFFBFFFF' '00040000' " Remove top border
'FFF7FFFF' '00080000' " Remove bottom border
'FFFEFFFF' '00010000' " Remove left border
'FFFDFFFF' '00020000' " Remove right border
And after all of this (even after set_table_for_first_display)
you just need to call a form or method called REFRESH
Since I wrote an implentation of class CL_GUI_ALV_GRID I dont use FORMs but methods :
METHOD REFRESH.
DATA ls_stable TYPE lvc_s_stbl.
ls_stable-row = 'X'.
ls_stable-col = 'X'.
CALL METHOD me->refresh_table_display
EXPORTING
is_stable = ls_stable
EXCEPTIONS
finished = 1
OTHERS = 2.
ENDMETHOD.
x and y are not mandatory fields so I can loop over all lines to set a property to a column (y is unset)
ASSIGN mt_outtab->* TO <tab>.
CHECK <tab> IS ASSIGNED.
LOOP AT <tab> ASSIGNING <line>.
update_style( x sy-tabix update_mask update_style )
or loop over my fieldcatalog to set a property to a line (x is unset)
LOOP at me->get_frontend_fieldcatalog( ) in ls_fcat
update_style( ls_fcat-fieldname y update_mask update_style )
For more possibilities, check link
ABAP Alv_grid Merge cells and style formating of cells
LOOP AT Lt_OUT.
IF lt_out-sum >= 50000.
lt_out-color = 'C311'.
ENDIF.
MODIFY Lt_OUT.
ENDLOOP.
layout-info_fieldname = 'COLOR".

How to draw a number of plots for one function with multiple parameters?

Let's say my altitude-pressure function is:
P(h) = p0 * exp(-h/scale)
I'd want to draw a set of plots for different planets; the same graph (canvas) but different p0 and scale parameters, a pair (plus name of the planet) per one planet.
Do I have to enter "multiplot" and reassign scale = and p0 = before calling the same plot P(h) for every set of parameters or is there a neater way to get a set of graphs like this?
You can define three different space-separated string which hold the parameters and then iterate over them:
p0 = "1 2 3 4"
scale = "0.1 0.2 0.3 0.4"
planets = "First Second Third Fourth"
P(h, n) = (1.0*word(p0, n)) * exp(-h/(1.0*word(scale, n)))
plot for [i=1:words(planets)] P(x, i) title word(planets, i)
The 1.0* is used to 'convert' the respective string to a number. Ugly, but works. If you want it a bit cleaner, you could define functions p0 and scale to return a number depending on an iteration parameter
p0(n) = (n==1 ? 1 : n==2 ? 2 : n==3 ? 3 : 4)
scale(n) = (n==1 ? 0.1 : n==2 ? 0.2 : n==3 ? 0.3 : 0.4)
P(h, n) = p0(n)*exp(-h/scale(n))
plot for [i=1:words(planets)] P(x, i) title word(planets, i)

Recreate ggplot's geom_smooth CI background - in R basic?

I wish to recreate this graph:
(from here)
Using R base graphics.
I have no clue how to do that. Any advice ?
(My motivation is that I wish to create a plot where the line width (and/or color) will reflect another dimension. Until now - ggplot2 is the only place I found in R for how to do this. I would be happy to be able to do this also in base R)
See help(polygon) and example(polygon) (esp the Brownian motion example) -- the varying width is pretty common in some fields to show variability through time.
The same example is also in demo(graphics):
## An example showing how to fill between curves.
par(bg="white")
n <- 100
x <- c(0,cumsum(rnorm(n)))
y <- c(0,cumsum(rnorm(n)))
xx <- c(0:n, n:0)
yy <- c(x, rev(y))
plot(xx, yy, type="n", xlab="Time", ylab="Distance")
polygon(xx, yy, col="gray")
title("Distance Between Brownian Motions")
I don't know if exactly replicating the graph is possible in base graphics. In grid graphics it is possible. Nevertheless, the following code gets you an example that's something like what you want. Adapt it to the data set.
n <- 20
x <- rnorm(n)
y <- rnorm(n)
o <- order(x)
x <- x[o]
y <- y[o]
m <- loess(y~x, span = 1) #ggplot seems to smooth more than default
f <- predict(m, se = TRUE)
ci <- f$se * qt(0.975, f$df)
cih <- f$fit + ci
cil <- f$fit - ci
plot(x,y, ylim = c(min(cil,y), max(cih,y)))
lines(x, f$fit, lwd = 2)
xx <- c(x, rev(x))
yy <- c(cil, rev(cih))
polygon(xx, yy, col="#A9A9A930", border = NA)
OK, I spent a little too much time messing with this... note the last line is the ggplot version so you can compare the two.
#loess and error curves almost just like ggplot2
op <- par(las=1, mar = c(3,3,1,1))
n <- 30
x <- sort(rnorm(n)) #(varying density in predictor)
x <- x + abs(min(x))
x <- x/max(x)*2*pi
y <- sin(x)+rnorm(n) #(curvy)
m <- loess(y~x)
xx <- seq(min(x), max(x), (max(x)-min(x))/1000) #increase density of values to predict over to increase quality of curve
f <- predict(m, xx, se = TRUE)
ci <- f$se * qt(0.975, f$df)
cih <- f$fit + ci
cil <- f$fit - ci
plot(x,y, ylim = c(min(cil,y), max(cih,y)), cex.axis = 0.85, xlab = '', ylab = '', type = 'n')
title(xlab = 'x', ylab = 'y',line = 2)
grid(col = 'gray')
points(x,y, pch = 19, cex = 0.65)
lines(xx, f$fit, col = 'blue', lwd = 1.2)
xx <- c(xx, rev(xx))
yy <- c(cil, rev(cih))
polygon(xx, yy, col=rgb(0.1,0.1,0.1,0.25), border = NA)
par(op)
#qplot(x,y, geom = 'point') + stat_smooth()
And to get the smooth curve, look at loess and predict.loess
Would geom_ribbon in GGPlot be what you need? This creates a variable-width line.

Resources